* Kristoffer Balle Hvidberg
* Field of Study and Financial Problems: How Economics Reduces the Risk of Default

********************************************************************************
clear all
set more off
novarabbrev

global data "K:\Workdata\704365\kbh\FSFP_Replication_Package\data"
global temp "K:\Workdata\704365\kbh\FSFP_Replication_Package\data\temp"
global raw  "K:\Workdata\704365\kbh\FSFP_Replication_Package\data\raw"
global pofi "K:\Workdata\704365\kbh\FSFP_Replication_Package\data\postfile"
global figs "K:\Workdata\704365\kbh\FSFP_Replication_Package\output\fig"
global tabs "K:\Workdata\704365\kbh\FSFP_Replication_Package\output\tab"

* NOTE: Delete this global and line 35 and change _dstpctile to _pctile in order to run outside Statistics Denmark's servers. 
global code "K:\workdata\704365\kbh\Projekt02\code"

global layout "plotregion(col(gs15) lc(white)) graphregion(color(white)) yscale(lc(white)) xscale(lc(white))"
global lblobs "labsize(small) grid glc(white)"

global if "f1!=f2 & !mi(f1) & !mi(f2) & completed_fields==1"

********************************************************************************

* Programs

	cap program drop times100
		program define times100, eclass
			mat b = e(b)*100
			mat V = e(V)*100^2
			ereturn repost b=b V=V
		end
		
	do "$code/_dstpctile_fast.ado"

	* install rdexo, cgmreg, unique, clustse

* Load data

	frame create binding_sample1
	frame change binding_sample1
	use "$data\analyse_del_3", clear
	replace over = -(over-1)
	
	frame create binding_sample2
	frame change binding_sample2
	use "$data\analyse_del_3", clear
	replace over = -(over-1)
	* Drop applicants with STEM as preferred and Medicine as alternative
	drop if f1==1
	drop if f2==7
	drop cf2_*_7 cf2_1_* zf2_*_7 zf2_1_*
	

	frame create first_time_applicants
	frame change first_time_applicants
	use "$data/analyse_first_time_applicants", clear

	frame create sum_stat
	
	frame create admission_thresholds
	frame change admission_thresholds
	* Only used in one footnote
	*use "$raw/kot_adgangskvotienter", clear
	*merge 1:1 kotnr year using "$temp/kotnr_niveau_field"
	
********************************************************************************
	
* TABEL 1 AND A3: Summary statistics

	frame change sum_stat
	
	use "$data\analyse_del_3", clear 

	gen pop3 = 1
	gen pop4 = 1 if $if
	keep pnr yoa pop*
	sort pnr yoa

	save "$temp\sample"

	use "$data\analyse_first_time_applicants", clear

	sort pnr yoa
	duplicates report pnr yoa
	duplicates report pnr
	merge 1:1 pnr yoa using "$temp\sample"

	gen vu_far=(audd_niveau_far=="Lange videregående uddannelser" | audd_niveau_far=="Ph.d. og forskeruddannelser") if !mi(audd_niveau_far)
	gen vu_mor=(audd_niveau_mor=="Lange videregående uddannelser" | audd_niveau_mor=="Ph.d. og forskeruddannelser") if !mi(audd_niveau_mor)

	replace sex      = sex*100
	replace vu_far   = vu_far*100
	replace vu_mor   = vu_mor*100
	replace restance = restance*100

	label var gpa "GPA"
	label var opt_prio "Offered rank"
	label var num_of_app "Number of applications"
	label var age "Age"
	label var sex "Male (\%)"
	label var inc_rank_45_far "Father's income rank"
	label var inc_rank_45_mor "Mother's income rank"
	label var inc_rank_10ya "Income rank"
	label var vu_far "Father has Master's (\%)"
	label var vu_mor "Mother has Master's (\%)"
	label var restance "Default (\%)"
	label var cut_first "First-priority threshold"

	local sumvars "age sex gpa cut_first opt_prio num_of_app inc_rank_10ya inc_rank_45_far inc_rank_45_mor vu_far vu_mor restance"
	eststo all     : qui estpost sum `sumvars'           , d
	eststo binding : qui estpost sum `sumvars' if pop3==1, d
	eststo sample  : qui estpost sum `sumvars' if pop4==1, d
	esttab all binding sample using "$tabs\Table1.tex", replace cells("mean(fmt(1))  sd(fmt(1)) ") ///
		label nonum nomtit collabels("Mean" "SD") ///
		mgroup("First-time applicants" "Binding threshold" "Sample", pattern(1 1 1) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
		
	local sumvars "age sex gpa cut_first opt_prio num_of_app inc_rank_10ya inc_rank_45_far inc_rank_45_mor vu_far vu_mor restance"
	eststo all     : qui estpost sum `sumvars'           , d
	eststo sample  : qui estpost sum `sumvars' if pop4==1, d
	esttab all sample using "$tabs\TableA3.tex", replace cells("count mean(fmt(1))  sd(fmt(1)) ") ///
		label nonum noobs nomtit collabels("Obs." "Mean" "SD") ///
		mgroup("1st time applicants" "Sample", pattern(1 1) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 

		
********************************************************************************

* TABLE A5

	frame change binding_sample1

	tab kotnavn1 if f1==2 & f2!=7 & $if
	tab kotnavn1 if f1==3 & f2!=7 & $if
	tab kotnavn1 if f1==4 & f2!=7 & $if
	tab kotnavn1 if f1==5 & f2!=7 & $if
	tab kotnavn1 if f1==6 & f2!=7 & $if
	tab kotnavn1 if f1==7 & f2!=7 & $if
	tab kotnavn1 if f1==8 & f2!=7 & $if

	tab kotnavn2 if f2==1 & f1!=1 & $if
	tab kotnavn2 if f2==2 & f1!=1 & $if
	tab kotnavn2 if f2==3 & f1!=1 & $if
	tab kotnavn2 if f2==4 & f1!=1 & $if
	tab kotnavn2 if f2==5 & f1!=1 & $if
	tab kotnavn2 if f2==6 & f1!=1 & $if
	tab kotnavn2 if f2==8 & f1!=1 & $if		
			
********************************************************************************

* TABLE A6

	mat X = J(9,8,.)
	local i = 1
	foreach x in 2 3 4 5 6  8 7 { // Preferred field
		local i = `i'+1
		local j = 0 
		foreach y in  2 3 4 5 6 8 1 { // Alternative field
			local j = `j'+1
			qui count if f1 == `x' & f2==`y' & $if
			mat X[`i',`j'] = r(N)
		}
		qui count if f1 == `x'  & f2!=7 & $if
		mat X[`i',8] = r(N)
	}
	local j = 0 
	foreach y in  2 3 4 5 6 8 1 { // Alternative field
		local j = `j'+1
		qui count if f2 == `y' & f1!=1 & $if
		mat X[9,`j'] = r(N)
	}
	count if !mi(f1) & !mi(f2) & f1!=1 & f2!=7 & $if
	mat X[9,8]=r(N)
	mat rowname X = "Preferred field" "\hspace{10pt}Bus. \& Econ." "\hspace{10pt}Law" "\hspace{10pt}Hum., Art \& Arch." "\hspace{10pt}Education" "\hspace{10pt}Welfare" "\hspace{10pt}Social Science" "\hspace{10pt}Medicine" "Total"
	mat colname X =  "B\&E" "Law" "HAA" "Educ." "Welf." "SocSci" "STEM" "Tot."

	mat X[9,8]=X[9,8]-X[2,4]-X[5,2]-X[2,5]-X[8,4] // TOTAL
	mat X[9,2]=X[9,2]-X[5,2]
	mat X[9,4]=X[9,4]-X[2,4]-X[8,4]
	mat X[9,5]=X[9,5]-X[2,5]
	mat X[5,8]=X[5,8]-X[5,2]
	mat X[2,8]=X[2,8]-X[2,4]-X[2,5]
	mat X[8,8]=X[8,8]-X[8,4]
	mat X[5,2]=.
	mat X[2,4]=.
	mat X[2,5]=.
	mat X[8,4]=.

	esttab matrix(X) using "$tabs\TableA6.tex", replace nomtit mgroup("Alternative field of study", pattern(1 0 0 0 0 0 0 1) span prefix(\multicolumn{7}{c}{) suffix(} &)  end(\cline{2-8})) 

********************************************************************************

* FIGURE 1 AND A1

	cap drop enrol1_1ya
	gen 	enrol1_1ya = 0
	foreach x in stem oeko jura huaa educ welf medi sosc {
		replace enrol1_1ya = 1 if kot1_`x'==1 & `x'_1ya==1
	}

	cap drop comp1
	gen comp1 = 0
	foreach x in stem oeko jura huaa educ welf medi sosc {
		forval y=5/10 {
			replace comp1 = 1 if kot1_`x'==1 & `x'_`y'ya==1
		}
	}

	cap drop enrol2_1ya
	gen 	enrol2_1ya = 0
	foreach x in stem oeko jura huaa educ welf medi sosc {
		replace enrol2_1ya = 1 if kot2_`x'==1 & `x'_1ya==1
	}

	cap drop comp2
	gen comp2 = 0
	foreach x in stem oeko jura huaa educ welf medi sosc {
		forval y=5/10 {
			replace comp2 = 1 if kot2_`x'==1 & `x'_`y'ya==1
		}
	}

	*Panel A
	cap drop at_vals
	cap drop L*
	cap drop R*
	gen at_vals = _n-31 if _n<62
	lpoly optaget1  	dist10 if dist10<0  & $if, deg(1) bw(5) kernel(rec) at(at_vals) gen(L1) nogr
	lpoly optaget1  	dist10 if dist10>=0 & $if, deg(1) bw(5) kernel(rec) at(at_vals) gen(R1) nogr
	lpoly enrol1_1ya 	dist10 if dist10<0  & $if, deg(1) bw(5) kernel(rec) at(at_vals) gen(L2) nogr
	lpoly enrol1_1ya 	dist10 if dist10>=0 & $if, deg(1) bw(5) kernel(rec) at(at_vals) gen(R2) nogr
	lpoly comp1 		dist10 if dist10<0  & $if, deg(1) bw(5) kernel(rec) at(at_vals) gen(L3) nogr
	lpoly comp1 		dist10 if dist10>=0 & $if, deg(1) bw(5) kernel(rec) at(at_vals) gen(R3) nogr

	cap postclose fig
	postfile fig dist10 opt en1 cmp str5 grph using "$pofi/genop", replace
	levelsof dist10 if inrange(dist10,-20,20), loc(b)
	foreach x in `b' {
		qui count if dist10==`x' & $if 
		if r(N)<=29 {
			di "FOR FÅ OBSERVATIONER"  
		}
		if r(N)>29 {
			qui reg optaget1   if dist10==`x' & $if 
			loc opt = _b[_cons]
			qui reg enrol1_1ya if dist10==`x' & $if 
			loc en1 = _b[_cons]
			qui reg comp1      if dist10==`x' & $if 
			loc cmp = _b[_cons]
			post fig (`x') (`opt') (`en1') (`cmp') ("scat")
		}
	}
	forval x = -20/20 {
		sum L1 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) (.) (.) ("L1") 
		sum R1 if at_vals==`x', meanonly     
		post fig (`x') (r(mean)) (.) (.) ("R1") 
		sum L2 if at_vals==`x', meanonly     
		post fig (`x') (.) (r(mean)) (.) ("L2") 
		sum R2 if at_vals==`x', meanonly     
		post fig (`x') (.) (r(mean)) (.) ("R2") 
		sum L3 if at_vals==`x', meanonly
		post fig (`x') (.) (.) (r(mean)) ("L3") 
		sum R3 if at_vals==`x', meanonly
		post fig (`x') (.) (.) (r(mean)) ("R3")
	}
	postclose fig
	drop L* R* at_vals
	
	* A1 
	preserve
		use "$pofi/genop", clear
		twoway 	(scatter opt dist10 if grph=="scat", msym(Oh) mc(blue*.5))  ///
				(scatter cmp dist10 if grph=="scat", msym(Oh) mc(red*.5)) ///
				(scatter en1 dist10 if grph=="scat", msym(Oh) mc(green*.5)) ///
				(line    opt dist10 if grph=="L1" & dist10<=0, lp(solid)  lc(gs5)) ///
				(line    opt dist10 if grph=="R1" & dist10>=0, lp(solid)  lc(gs5)) ///
				(line    cmp dist10 if grph=="L3" & dist10<=0, lp(dash) lc(gs5)) ///
				(line    cmp dist10 if grph=="R3" & dist10>=0, lp(dash) lc(gs5)) ///
				(line    en1 dist10 if grph=="L2" & dist10<=0, lp(longdash_dot) lc(gs5)) ///
				(line    en1 dist10 if grph=="R2" & dist10>=0, lp(longdash_dot) lc(gs5)) ///
				,  ///
					xline(0, lp(dash) lc(black)) $layout ///
					ylabel(0(.2)1, $lblobs)  ///
					xlabel(-20 "-2" -10 "-1" 0 "0" 10 "1" 20 "2", $lblobs) ///
					xtitle("Distance from threshold") ytitle("Share admitted, enrolled and completed preferred field") ///
					legend(label(1 "Admitted") label(2 "Completed") label(3 "Enrolled 2nd year") order(1 3 2) r(3) ring(0) pos(5)) ///
					xsize(20) ysize(20) name(fig1, replace)
	restore
	
	* 1 
	preserve
		use "$pofi/genop", clear
		twoway 	(scatter opt dist10 if grph=="scat", msym(Oh) mc(blue*.5))  ///
				(line    opt dist10 if grph=="L1" & dist10<=0, lp(solid)  lc(gs5)) ///
				(line    opt dist10 if grph=="R1" & dist10>=0, lp(solid)  lc(gs5)) ///
				,  ///
					xline(0, lp(dash) lc(black)) $layout ///
					ylabel(0(.2)1, $lblobs)  ///
					xlabel(-20 "-2" -10 "-1" 0 "0" 10 "1" 20 "2", $lblobs) ///
					xtitle("Distance from threshold") ytitle("Share admitted to preferred field") ///
					legend(off) ///
					xsize(20) ysize(20) name(fig2, replace)
	restore

	* Panel B
	cap drop at_vals
	gen at_vals = _n-31 if _n<62
	lpoly optaget2  	dist10 if dist10<0  & $if, deg(1) bw(5) kernel(rec) at(at_vals) gen(L1) nogr
	lpoly optaget2  	dist10 if dist10>=0 & $if, deg(1) bw(5) kernel(rec) at(at_vals) gen(R1) nogr
	lpoly enrol2_1ya 	dist10 if dist10<0  & $if, deg(1) bw(5) kernel(rec) at(at_vals) gen(L2) nogr
	lpoly enrol2_1ya 	dist10 if dist10>=0 & $if, deg(1) bw(5) kernel(rec) at(at_vals) gen(R2) nogr
	lpoly comp2 		dist10 if dist10<0  & $if, deg(1) bw(5) kernel(rec) at(at_vals) gen(L3) nogr
	lpoly comp2			dist10 if dist10>=0 & $if, deg(1) bw(5) kernel(rec) at(at_vals) gen(R3) nogr

	cap postclose fig
	postfile fig dist10 opt en1 cmp str5 grph using "$pofi/genop", replace
	levelsof dist10 if inrange(dist10,-20,20), loc(b)
	foreach x in `b' {
		qui count if dist10==`x' & $if
		if r(N)<=29 {
			di "FOR FÅ OBSERVATIONER"  
		}
		if r(N)>29 {
			qui reg optaget2   if dist10==`x' & $if
			loc opt = _b[_cons]
			qui reg enrol2_1ya if dist10==`x' & $if
			loc en1 = _b[_cons]
			qui reg comp2      if dist10==`x' & $if
			loc cmp = _b[_cons]
			post fig (`x') (`opt') (`en1') (`cmp') ("scat")
		}
	}
	forval x = -20/20 {
		sum L1 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) (.) (.) ("L1") 
		sum R1 if at_vals==`x', meanonly     
		post fig (`x') (r(mean)) (.) (.) ("R1") 
		sum L2 if at_vals==`x', meanonly     
		post fig (`x') (.) (r(mean)) (.) ("L2") 
		sum R2 if at_vals==`x', meanonly     
		post fig (`x') (.) (r(mean)) (.) ("R2") 
		sum L3 if at_vals==`x', meanonly
		post fig (`x') (.) (.) (r(mean)) ("L3") 
		sum R3 if at_vals==`x', meanonly
		post fig (`x') (.) (.) (r(mean)) ("R3")
	}
	postclose fig
	drop L* R* at_vals
	
	* A1
	preserve
		use "$pofi/genop", clear
		twoway 	(scatter opt dist10 if grph=="scat", msym(Oh) mc(blue*.5))  ///
				(scatter cmp dist10 if grph=="scat", msym(Oh) mc(red*.5)) ///
				(scatter en1 dist10 if grph=="scat", msym(Oh) mc(green*.5)) ///
				(line    opt dist10 if grph=="L1" & dist10<=0, lp(solid)  lc(gs5)) ///
				(line    opt dist10 if grph=="R1" & dist10>=0, lp(solid)  lc(gs5)) ///
				(line    cmp dist10 if grph=="L3" & dist10<=0, lp(dash) lc(gs5)) ///
				(line    cmp dist10 if grph=="R3" & dist10>=0, lp(dash) lc(gs5)) ///
				(line    en1 dist10 if grph=="L2" & dist10<=0, lp(longdash_dot) lc(gs5)) ///
				(line    en1 dist10 if grph=="R2" & dist10>=0, lp(longdash_dot) lc(gs5)) ///
				,  ///
					xline(0, lp(dash) lc(black)) $layout ///
					ylabel(0(.2)1, $lblobs)  ///
					xlabel(-20 "-2" -10 "-1" 0 "0" 10 "1" 20 "2", $lblobs) ///
					xtitle("Distance from threshold") ytitle("Share admitted, enrolled and completed alternative field") ///
					legend(label(1 "Admitted") label(2 "Completed") label(3 "Enrolled 2nd year") order(1 3 2) r(3) ring(0) pos(1)) ///
					xsize(20) ysize(20) name(fig3, replace)
	restore
	
	* 1 
	preserve
		use "$pofi/genop", clear
		twoway 	(scatter opt dist10 if grph=="scat", msym(Oh) mc(blue*.5))  ///
				(line    opt dist10 if grph=="L1" & dist10<=0, lp(solid)  lc(gs5)) ///
				(line    opt dist10 if grph=="R1" & dist10>=0, lp(solid)  lc(gs5)) ///
				,  ///
					xline(0, lp(dash) lc(black)) $layout ///
					ylabel(0(.2)1, $lblobs)  ///
					xlabel(-20 "-2" -10 "-1" 0 "0" 10 "1" 20 "2", $lblobs) ///
					xtitle("Distance from threshold") ytitle("Share admitted to alternative field") ///
					legend(off) ///
					xsize(20) ysize(20) name(fig4, replace)
	restore	
	
	graph export "$figs\FigureA1A.pdf", replace name(fig1)
	graph export "$figs\Figure1A.pdf",  replace name(fig2)
	graph export "$figs\FigureA1B.pdf", replace name(fig3)
	graph export "$figs\Figure1B.pdf",  replace name(fig4)
	graph close
		
********************************************************************************
	
* FIGURE A2

	frame change first_time_applicants
	
	* Panel A
	sum restance if !mi(audd_n)

	cap postclose barplot
	postfile barplot c b h l using "$pofi\barplot", replace
	forval x=1/8 {
		reg restance if audd_n==`x'
		post barplot (`x') (_b[_cons]) (_b[_cons]+1.65*_se[_cons]) (_b[_cons]-1.65*_se[_cons])
	}
	postclose barplot

	sum restance if audd_n==7
	sum restance if audd_n==4
	sum restance if audd_n==2
	sum restance if audd_field2_10ya=="Economics"  
	sum restance if audd_field3_10ya=="Business and administration" 

	preserve
		use "$pofi\barplot", clear
		replace c = -8 if c == 4
		replace c = -7 if c == 2
		replace c = -6 if c == 6
		replace c = -5 if c == 8
		replace c = -4 if c == 1
		replace c = -3 if c == 5
		replace c = -2 if c == 3
		replace c = -1 if c == 7
		
		twoway 	(bar b c if c<0, barw(.5) col(blue*.5)) ///
				(rcap h l c if c<0, col(gs5)), ///
				$layout xsize(20) ysize(20) legend(off) ///
				xlabel(-8 "Hum., Art & Arch." -7 "Bus. and Econ." -6 "Welfare" -5 "Social Science" -4 "STEM" -3 "Education" -2 "Law" -1 "Medicine" , angle(45) $lblobs) ///
				ylabel(0(.02).12, $lblobs) ///
				ytitle("Share of applicants experiencing default") xtitle("") ///
				name(fig1, replace)
	restore
	
	
	* Panel B	
	sum blb_rest8_2016 if blb_rest8_2016, d
	
	cap drop x
	gen 	x=blb_rest8_2016
	replace x=400001 if x>400001 & !mi(x)
	replace x=. if x==0
	replace x=x/1000
	
	sum x, d
	
	hist x, ///
		freq w(10) start(0)  fcol(blue*.5) lcol(blue*.6) ///
		$layout xsize(20) ysize(20) ///
		xlabel(,$lblobs) ///
		ylabel(0 "0" 5000 "5" 10000 "10" 15000 "15" 20000 "20",$lblobs) ///
		xtitle("Amount in default in 2016 (1000 DKK)") ///
		ytitle("Frequency (1000 applicants)") ///
		name(fig2, replace)
		
	count if inrange(x,390,400)
		
	* Panel C 
	frame change binding_sample1
	
	cap postclose event
	postfile event x b h l g using "$pofi/event", replace
	forval x=1/15 {
		qui reg rest8_`x'ya if $if
		post event (`x') (_b[_cons]) (_b[_cons]+1.65*_se[_cons]) (_b[_cons]-1.65*_se[_cons]) (1)
		qui reg rest8_`x'ya if f2==2 & $if
		post event (`x') (_b[_cons]) (_b[_cons]+1.65*_se[_cons]) (_b[_cons]-1.65*_se[_cons]) (2)
	}
	postclose event

	preserve 
		use "$pofi/event", clear
		replace x =x-0.15 if g==1
		replace x =x+0.15 if g==2
		twoway 	(scatter b x if g==1, lp(solid) color(gs5) ms(o) mc(blue*.5) ) ///
				(rcap    h l x if g==1, color(gs5%20) col(blue*.5)) ///
				(scatter  b x if g==2, lp(solid) ms(o) mc(red*.5) ) ///
				(rcap    h l x if g==2, col(red*.5)), ///
				$layout xsize(20) ysize(20) ///
				legend(ring(0) pos(11) r(2) label(1 "All applicants") label(2 "90% CI") label(3 "B&E as alternative") label(4 "90% CI") symx(5)) ///
				xtitle("Years since year of application") ytitle("Share of applicants in default") ///
				xlabel(0(3)15, $lblobs) ylabel(-0.005(.005).032,$lblobs) ///
				name(fig3, replace)
	restore
		
	graph export "$figs\FigureA2A.pdf", replace name(fig1)
	graph export "$figs\FigureA2B.pdf", replace name(fig2)
	graph export "$figs\FigureA2C.pdf", replace name(fig3)
	graph close

********************************************************************************
	
* FIGURE 2

	frame change first_time_applicants

	* Panel A
	cap postclose binscat
	postfile binscat y y_h y_l x using "$pofi/binscat", replace
	cap drop bin
	local bins=20
	local yvar "restance"
	local xvar "inc_rank_10ya"
	xtile bin = `xvar' if !mi(`yvar'), n(`bins')
	forval x=1/`bins' {
		qui sum `xvar' if bin==`x', meanonly
		local avg = r(mean)
		qui reg `yvar' if bin==`x'
		post binscat (_b[_cons]) (_b[_cons]+1.65*_se[_cons]) (_b[_cons]-1.65*_se[_cons]) (`avg')
	}
	postclose binscat

	preserve
		use "$pofi/binscat", clear
		twoway	(scatter y x, col(blue*.5) sym(o)) ///
				(rcap y_h y_l x, col(blue*.5)), ///
				$layout xsize(20) ysize(20) legend(off) ///
				ylabel(0(.05).3, $lblobs) xlabel(0(20)100, $lblobs) ///
				ytitle("Share of applicants experiencing default") xtitle("Within cohort income rank") ///
				name(fig1, replace)
	restore
	
	* Panel B
	cap postclose binscat
	postfile binscat y y_h y_l x using "$pofi/binscat", replace
	cap drop bin
	local bins=20
	local yvar "restance"
	local xvar "max_rente_10ya"
	xtile bin = `xvar' if !mi(`yvar'), n(`bins')
	forval x=1/`bins' {
		qui sum `xvar' if bin==`x', meanonly
		local avg = r(mean)
		qui reg `yvar' if bin==`x'
		post binscat (_b[_cons]) (_b[_cons]+1.65*_se[_cons]) (_b[_cons]-1.65*_se[_cons]) (`avg')
	}
	postclose binscat

	preserve
		use "$pofi/binscat", clear
		twoway	(rcap y_h y_l x if x<.4, col(blue*.5)) ///
				(scatter y x if x<.4, msym(o) mc(blue*.5)), ///
				$layout xsize(20) ysize(20) legend(off) ///
				ylabel(0(.05).3, $lblobs) xlabel(0(.05).3, $lblobs) ///
				ytitle("Share of applicants experiencing default") xtitle("Marginal interest rate") ///
				name(fig2, replace)
	restore

	graph export "$figs/Figure2A.pdf", replace name(fig1)
	graph export "$figs/Figure2B.pdf", replace name(fig2)
	graph close

********************************************************************************

* FIGURE 3 and A3

	frame change binding_sample2

	local if "f2==2 & (optaget1==1 | optaget2==1) & $if"
	local y "restance"
	local ylbl "0(.05).2"
	local xlbl "-15 "-1.5" -10 "-1" -5 "-0.5" 0 "0" 5 "0.5" 10 "1" 15 "1.5""
	local obs=29
	local dot "msym(O) mc(blue*.5)"

	* Figure 3 and panel E of Figure A3
	cap drop bin
	gen 	bin = .
	replace bin = 1  if `if' & inrange(dist10,-20,-19)
	replace bin = 2  if `if' & inrange(dist10,-18,-17)
	replace bin = 3  if `if' & inrange(dist10,-16,-15)
	replace bin = 4  if `if' & inrange(dist10,-14,-13)
	replace bin = 5  if `if' & inrange(dist10,-12,-11)
	replace bin = 6  if `if' & inrange(dist10,-10,-9)
	replace bin = 7  if `if' & inrange(dist10,-8 ,-7)
	replace bin = 8  if `if' & inrange(dist10,-6 ,-5)
	replace bin = 9  if `if' & inrange(dist10,-4 ,-3)
	replace bin = 10 if `if' & inrange(dist10,-2 ,-1)
	replace bin = 11 if `if' & inrange(dist10, 0 ,1)
	replace bin = 12 if `if' & inrange(dist10, 2 ,3)
	replace bin = 13 if `if' & inrange(dist10, 4 ,5)
	replace bin = 14 if `if' & inrange(dist10, 6 ,7)
	replace bin = 15 if `if' & inrange(dist10, 8 ,9)
	replace bin = 16 if `if' & inrange(dist10,10 ,11)
	replace bin = 17 if `if' & inrange(dist10,12 ,13)
	replace bin = 18 if `if' & inrange(dist10,14 ,15)
	replace bin = 19 if `if' & inrange(dist10,16 ,17)
	replace bin = 20 if `if' & inrange(dist10,18 ,19)
	cap drop at_vals
	gen at_vals = _n-21 if _n<42
	lpoly optaget2 dist10 if dist10<0  & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(L1) nogr se(L1se)
	lpoly optaget2 dist10 if dist10>=0 & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(R1) nogr se(R1se)
	lpoly `y'      dist10 if dist10<0  & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(L2) nogr se(L2se)
	lpoly `y'      dist10 if dist10>=0 & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(R2) nogr se(R2se)
	cap postclose fig
	postfile fig x y str5 grph using "$pofi/rd", replace
	levelsof bin, loc(b)
	foreach x in `b' {
		qui count if bin==`x' 
		if r(N)<=`obs' {
			di "X=`x': FOR FÅ OBSERVATIONER"  
		}
		if r(N)>`obs' {
			sum dist10 if bin==`x', meanonly
			local avg = r(mean)
			qui reg optaget2 if bin==`x' & `if'
			post fig (`avg') (_b[_cons]) ("scat1")
			qui reg `y' if bin==`x' & `if'
			post fig (`avg') (_b[_cons]) ("scat2")
		}
	}
	forval x = -20/20 {
		sum L1 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L1") 
		sum R1 if at_vals==`x', meanonly 
		post fig (`x') (r(mean)) ("R1") 
		sum L2 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L2") 
		sum R2 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("R2") 
		sum L1se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L1se") 
		sum R1se if at_vals==`x', meanonly 
		post fig (`x') (r(mean)) ("R1se") 
		sum L2se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L2se") 
		sum R2se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("R2se") 
	}
	postclose fig
	drop L1* R1* L2* R2* at_vals
		
	preserve
		use "$pofi/rd", clear
		gen y1 = y + 1.65*y[_n+4]
		gen y2 = y - 1.65*y[_n+4]
		twoway 	(rarea 	 y1 y2 x if grph=="L1" & x<=0 & x>=-15, color(gs5%20)) ///
				(rarea 	 y1 y2 x if grph=="R1" & x>=0 & x<=15,  color(gs5%20)) ///
				(scatter y x if grph=="scat1", `dot')  ///
				(line    y x if grph=="L1" & x<=0 & x>=-15, lp(solid) lc(gs5)) ///
				(line    y x if grph=="R1" & x>=0 & x<=15,  lp(solid) lc(gs5)) ///
				,  ///
					xline(0, lp(dash) lc(black)) $layout ///
					ylabel(0(.25)1, $lblobs)  ///
					xlabel(`xlbl', $lblobs) ///
					xtitle("Distance from threshold") ytitle("Share admitted to {it:Business and economics}") ///
					legend(off) ///
					xsize(20) ysize(11) name(fig1, replace)
		replace y1=0.2 if y1>0.2
		replace y2=0   if y2<0
		twoway 	(rarea 	 y1 y2 x if grph=="L2" & x<=0 & x>=-15, color(gs5%20)) ///
				(rarea 	 y1 y2 x if grph=="R2" & x>=0 & x<=15,  color(gs5%20)) ///
				(scatter y     x if grph=="scat2", `dot')  ///
				(line    y     x if grph=="L2" & x<=0 & x>=-15, lp(solid) lc(gs5)) ///
				(line    y     x if grph=="R2" & x>=0 & x<=15,  lp(solid) lc(gs5)) ///
				,  ///
					xline(0, lp(dash) lc(black)) $layout ///
					ylabel(`ylbl', $lblobs)  ///
					xlabel(`xlbl', $lblobs) ///
					xtitle("Distance from threshold") ytitle("Share of applicants in default") ///
					legend(ring(0) pos(1) order(3 4 1) label(3 "Bin average") label(1 "90% CI") label(4 "Local linear polynomial") r(3)) ///
					xsize(20) ysize(11) name(fig2, replace)
	restore

	* Figure A3 Panel A and B
	cap drop bin
	cap drop bin1 bin2
	xtile bin1 = dist10 if `if' & dist10<0,  n(4)
	xtile bin2 = dist10 if `if' & dist10>=0, n(4)
	gen 	bin = -bin1
	replace bin = bin2 if mi(bin)
	drop bin1 bin2
	cap drop at_vals
	gen at_vals = _n-21 if _n<42
	lpoly optaget2 dist10 if dist10<0  & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(L1) nogr se(L1se)
	lpoly optaget2 dist10 if dist10>=0 & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(R1) nogr se(R1se)
	lpoly `y'      dist10 if dist10<0  & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(L2) nogr se(L2se)
	lpoly `y'      dist10 if dist10>=0 & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(R2) nogr se(R2se)
	cap postclose fig
	postfile fig x y str5 grph using "$pofi/rd", replace
	levelsof bin, loc(b)
	foreach x in `b' {
		qui count if bin==`x' 
		if r(N)<=`obs' {
			di "X=`x': FOR FÅ OBSERVATIONER"  
		}
		if r(N)>`obs' {
			sum dist10 if bin==`x', meanonly
			local avg = r(mean)
			qui reg optaget2 if bin==`x' & `if'
			post fig (`avg') (_b[_cons]) ("scat1")
			qui reg `y' if bin==`x' & `if'
			post fig (`avg') (_b[_cons]) ("scat2")
		}
	}
	forval x = -20/20 {
		sum L1 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L1") 
		sum R1 if at_vals==`x', meanonly 
		post fig (`x') (r(mean)) ("R1") 
		sum L2 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L2") 
		sum R2 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("R2") 
		sum L1se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L1se") 
		sum R1se if at_vals==`x', meanonly 
		post fig (`x') (r(mean)) ("R1se") 
		sum L2se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L2se") 
		sum R2se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("R2se") 
	}
	postclose fig
	drop L1* R1* L2* R2* at_vals
		
	preserve
		use "$pofi/rd", clear
		gen y1 = y + 1.65*y[_n+4]
		gen y2 = y - 1.65*y[_n+4]
		twoway 	(rarea 	 y1 y2 x if grph=="L1" & x<=0 & x>=-15, color(gs5%20)) ///
				(rarea 	 y1 y2 x if grph=="R1" & x>=0 & x<=15,  color(gs5%20)) ///
				(scatter y x if grph=="scat1", `dot')  ///
				(line    y x if grph=="L1" & x<=0 & x>=-15, lp(solid) lc(gs5)) ///
				(line    y x if grph=="R1" & x>=0 & x<=15,  lp(solid) lc(gs5)) ///
				,  ///
					xline(0, lp(dash) lc(black)) $layout ///
					ylabel(0(.25)1, $lblobs)  ///
					xlabel(`xlbl', $lblobs) ///
					xtitle("Distance from threshold") ytitle("Share admitted to {it:Business and economics}") ///
					legend(off) ///
					xsize(20) ysize(11) name(fig3, replace)
		replace y1=0.2 if y1>0.2
		replace y2=0   if y2<0
		twoway 	(rarea 	 y1 y2 x if grph=="L2" & x<=0 & x>=-15, color(gs5%20)) ///
				(rarea 	 y1 y2 x if grph=="R2" & x>=0 & x<=15,  color(gs5%20)) ///
				(scatter y     x if grph=="scat2", `dot')  ///
				(line    y     x if grph=="L2" & x<=0 & x>=-15, lp(solid) lc(gs5)) ///
				(line    y     x if grph=="R2" & x>=0 & x<=15,  lp(solid) lc(gs5)) ///
				,  ///
					xline(0, lp(dash) lc(black)) $layout ///
					ylabel(`ylbl', $lblobs)  ///
					xlabel(`xlbl', $lblobs) ///
					xtitle("Distance from threshold") ytitle("Share of applicants in default") ///
					legend(off) ///
					xsize(20) ysize(11) name(fig4, replace)
	restore

	* Figure A3 Panel C and D
	cap drop bin
	gen bin = dist10 if inrange(dist10,-15,15) & `if'
	cap drop at_vals
	gen at_vals = _n-21 if _n<42
	lpoly optaget2 dist10 if dist10<0  & `if', deg(1) bw(10) kernel(rec) at(at_vals) gen(L1) nogr se(L1se)
	lpoly optaget2 dist10 if dist10>=0 & `if', deg(1) bw(10) kernel(rec) at(at_vals) gen(R1) nogr se(R1se)
	lpoly `y'      dist10 if dist10<0  & `if', deg(1) bw(10) kernel(rec) at(at_vals) gen(L2) nogr se(L2se)
	lpoly `y'      dist10 if dist10>=0 & `if', deg(1) bw(10) kernel(rec) at(at_vals) gen(R2) nogr se(R2se)
	cap postclose fig
	postfile fig x y str5 grph using "$pofi/rd", replace
	levelsof bin, loc(b)
	foreach x in `b' {
		qui count if bin==`x' 
		if r(N)<=`obs' {
			di "X=`x': FOR FÅ OBSERVATIONER"  
		}
		if r(N)>`obs' {
			sum dist10 if bin==`x', meanonly
			local avg = r(mean)
			qui reg optaget2 if bin==`x' & `if'
			post fig (`avg') (_b[_cons]) ("scat1")
			qui reg `y' if bin==`x' & `if'
			post fig (`avg') (_b[_cons]) ("scat2")
		}
	}
	forval x = -15/15 {
		sum L1 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L1") 
		sum R1 if at_vals==`x', meanonly 
		post fig (`x') (r(mean)) ("R1") 
		sum L2 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L2") 
		sum R2 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("R2") 
		sum L1se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L1se") 
		sum R1se if at_vals==`x', meanonly 
		post fig (`x') (r(mean)) ("R1se") 
		sum L2se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L2se") 
		sum R2se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("R2se") 
	}
	postclose fig
	drop L1* R1* L2* R2* at_vals
		
		preserve
		use "$pofi/rd", clear
		gen y1 = y + 1.65*y[_n+4]
		gen y2 = y - 1.65*y[_n+4]
		twoway 	(rarea 	 y1 y2 x if grph=="L1" & x<=0 & x>=-15, color(gs5%20)) ///
				(rarea 	 y1 y2 x if grph=="R1" & x>=0 & x<=15,  color(gs5%20)) ///
				(scatter y x if grph=="scat1", `dot')  ///
				(line    y x if grph=="L1" & x<=0 & x>=-15, lp(solid) lc(gs5)) ///
				(line    y x if grph=="R1" & x>=0 & x<=15,  lp(solid) lc(gs5)) ///
				,  ///
					xline(0, lp(dash) lc(black)) $layout ///
					ylabel(0(.25)1, $lblobs)  ///
					xlabel(`xlbl', $lblobs) ///
					xtitle("Distance from threshold") ytitle("Share admitted to {it:Business and economics}") ///
					legend(off) ///
					xsize(20) ysize(11) name(fig5, replace)
		replace y1=0.2 if y1>0.2
		replace y2=0   if y2<0
		twoway 	(rarea 	 y1 y2 x if grph=="L2" & x<=0 & x>=-15, color(gs5%20)) ///
				(rarea 	 y1 y2 x if grph=="R2" & x>=0 & x<=15,  color(gs5%20)) ///
				(scatter y     x if grph=="scat2", `dot')  ///
				(line    y     x if grph=="L2" & x<=0 & x>=-15, lp(solid) lc(gs5)) ///
				(line    y     x if grph=="R2" & x>=0 & x<=15,  lp(solid) lc(gs5)) ///
				,  ///
					xline(0, lp(dash) lc(black)) $layout ///
					ylabel(`ylbl', $lblobs)  ///
					xlabel(`xlbl', $lblobs) ///
					xtitle("Distance from threshold") ytitle("Share of applicants in default") ///
					legend(off) ///
					xsize(20) ysize(11) name(fig6, replace)
	restore


	* Figure A3 Panel F
	local if "f2==2 & (optaget1==1 | optaget2==1) & $if"
	local y "restance_hat"
	local ylbl "0(.05).2"
	local xlbl "-15 "-1.5" -10 "-1" -5 "-0.5" 0 "0" 5 "0.5" 10 "1" 15 "1.5""
	local obs=29
	local dot "msym(O) mc(blue*.5)"
	
	cap drop restance_hat
	probit restance i.sex i.yoa i.aldersgrp gpa if `if'
	predict restance_hat

	cap drop bin
	gen 	bin = .
	replace bin = 1  if `if' & inrange(dist10,-20,-19)
	replace bin = 2  if `if' & inrange(dist10,-18,-17)
	replace bin = 3  if `if' & inrange(dist10,-16,-15)
	replace bin = 4  if `if' & inrange(dist10,-14,-13)
	replace bin = 5  if `if' & inrange(dist10,-12,-11)
	replace bin = 6  if `if' & inrange(dist10,-10,-9)
	replace bin = 7  if `if' & inrange(dist10,-8 ,-7)
	replace bin = 8  if `if' & inrange(dist10,-6 ,-5)
	replace bin = 9  if `if' & inrange(dist10,-4 ,-3)
	replace bin = 10 if `if' & inrange(dist10,-2 ,-1)
	replace bin = 11 if `if' & inrange(dist10, 0 ,1)
	replace bin = 12 if `if' & inrange(dist10, 2 ,3)
	replace bin = 13 if `if' & inrange(dist10, 4 ,5)
	replace bin = 14 if `if' & inrange(dist10, 6 ,7)
	replace bin = 15 if `if' & inrange(dist10, 8 ,9)
	replace bin = 16 if `if' & inrange(dist10,10 ,11)
	replace bin = 17 if `if' & inrange(dist10,12 ,13)
	replace bin = 18 if `if' & inrange(dist10,14 ,15)
	replace bin = 19 if `if' & inrange(dist10,16 ,17)
	replace bin = 20 if `if' & inrange(dist10,18 ,19)
	cap drop at_vals
	gen at_vals = _n-21 if _n<42
	lpoly optaget2 dist10 if dist10<0  & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(L1) nogr se(L1se)
	lpoly optaget2 dist10 if dist10>=0 & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(R1) nogr se(R1se)
	lpoly `y'      dist10 if dist10<0  & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(L2) nogr se(L2se)
	lpoly `y'      dist10 if dist10>=0 & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(R2) nogr se(R2se)
	cap postclose fig
	postfile fig x y str5 grph using "$pofi/rd", replace
	levelsof bin, loc(b)
	foreach x in `b' {
		qui count if bin==`x' 
		if r(N)<=`obs' {
			di "X=`x': FOR FÅ OBSERVATIONER"  
		}
		if r(N)>`obs' {
			sum dist10 if bin==`x', meanonly
			local avg = r(mean)
			qui reg optaget2 if bin==`x' & `if'
			post fig (`avg') (_b[_cons]) ("scat1")
			qui reg `y' if bin==`x' & `if'
			post fig (`avg') (_b[_cons]) ("scat2")
		}
	}
	forval x = -15/15 {
		sum L1 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L1") 
		sum R1 if at_vals==`x', meanonly 
		post fig (`x') (r(mean)) ("R1") 
		sum L2 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L2") 
		sum R2 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("R2") 
		sum L1se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L1se") 
		sum R1se if at_vals==`x', meanonly 
		post fig (`x') (r(mean)) ("R1se") 
		sum L2se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L2se") 
		sum R2se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("R2se") 
	}
	postclose fig
	drop L1* R1* L2* R2* at_vals
		
	preserve
		use "$pofi/rd", clear
		gen y1 = y + 1.65*y[_n+4]
		gen y2 = y - 1.65*y[_n+4]
		replace y1=0.2 if y1>0.2
		replace y2=0   if y2<0
		twoway 	(rarea 	 y1 y2 x if grph=="L2" & x<=0, color(gs5%20)) ///
				(rarea 	 y1 y2 x if grph=="R2" & x>=0, color(gs5%20)) ///
				(scatter y     x if grph=="scat2", `dot')  ///
				(line    y     x if grph=="L2" & x<=0, lp(solid) lc(gs5)) ///
				(line    y     x if grph=="R2" & x>=0, lp(solid) lc(gs5)) ///
				,  ///
					xline(0, lp(dash) lc(black)) $layout ///
					ylabel(`ylbl', $lblobs)  ///
					xlabel(`xlbl', $lblobs) ///
					xtitle("Distance from threshold") ytitle("Predicted probability of default") ///
					legend(off) ///
					xsize(20) ysize(11) name(fig7, replace)
	restore

	graph export "$figs\FigureA3A.pdf", replace name(fig1)  
	graph export "$figs\Figure3.pdf",   replace name(fig2)  
	graph export "$figs\FigureA3C.pdf", replace name(fig3)  
	graph export "$figs\FigureA3D.pdf", replace name(fig4) 
	graph export "$figs\FigureA3E.pdf", replace name(fig5)  
	graph export "$figs\FigureA3F.pdf", replace name(fig6)  
	graph export "$figs\FigureA3B.pdf", replace name(fig7)
	graph close
	
********************************************************************************

* TABLE 2

	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	
	qui reg optaget2 over dist over_X_dist `if' `opt'
		times100
		eststo reg1
		estadd scalar avg=_b[_cons] 
	qui reg restance over dist over_X_dist `if' `opt'
		times100
		eststo reg2
		estadd scalar avg=_b[_cons]
	qui reg restance over dist over_X_dist i.f1 sex i.yoa i.aldersgrp `if' `opt'
		times100
		eststo reg3
		qui estadd local pre "\checkmark"
		qui estadd local con1 "\checkmark"
		qui estadd local con2 "\checkmark"
	qui reg restance over dist over_X_dist i.f1 sex i.yoa i.aldersgrp i.inc_dum `if' `opt'
		times100
		eststo reg4
		qui estadd local pre "\checkmark"
		qui estadd local con1 "\checkmark"
		qui estadd local con2 "\checkmark"
		qui estadd local inc "\checkmark"
	qui ivregress 2sls restance (optaget2 = over) dist over_X_dist  `if' `opt' first
		times100
		eststo reg5
	frame change first_time_applicants
	cap drop over
	gen over = (kot_navn_opt=="Økonomi" | kot_navn_opt=="Almen erhvervsøkonomi(HA)" | kot_navn_opt=="Erhvervsøkonomi-erhvervsret") 
	qui reg restance over sex 
		times100
		eststo reg6
		qui estadd local con1 "\checkmark"
	frame change binding_sample2

	esttab reg1 reg2 reg3 reg4 reg5 reg6 using "$tabs/Table2.tex", replace ///
		b(1) se nonote keep(over) ///
		scalar("avg Baseline" "pre Pref. field FE" "con1 Male (=1)" "con2 Year \& age FE" "inc Income") star(* 0.1 ** 0.05 *** 0.01)  ///
		mgroup("B\&E (\%)" "Probability of default (\%)", pattern(1 1 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) end(\cmidrule(lr){2-2}\cmidrule(lr){3-7}))  ///
		rename(optaget2 over) ///
		coef(over "$\mathbbm{1}$(Economics)") ///
		mtit("1st stage" "Reduced form" "Reduced form" "Reduced form" "LATE" "OLS")
		
********************************************************************************
	
* TABLE A7

	* (1) Include non-completers and multiple field completers
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & f1!=f2 & !mi(f1) & !mi(f2)"
	local opt ", robust"
	qui reg optaget2 over dist over_X_dist `if' `opt'
	local a = _b[over]
	local A = _se[over]^2
	qui reg restance over dist over_X_dist `if' `opt'
	local b = _b[over]
	local B = _se[over]^2
	local avg=_b[_cons]
	qui ivregress 2sls restance (optaget2 = over) dist over_X_dist `if' `opt'
	local c = _b[optaget2]
	local C = _se[optaget2]^2
	local N = e(N)

	mat b=J(1,3,.)
	mat V=J(3,3,0)
	mat b[1,1] = `a'
	mat b[1,2] = `b'
	mat b[1,3] = `c'
	mat V[1,1] = `A'
	mat V[2,2] = `B'
	mat V[3,3] = `C'
	mat colnames b = a b c
	mat colnames V = a b c
	mat rownames V = a b c

	ereturn post b V, depname(restance) obs(`N')
	times100
	eststo reg1
	qui estadd local run "Linear"
	qui estadd local bw "1.5"
	qui estadd local ker "Rect."
	qui estadd local non "\checkmark"
	qui estadd scalar bas=`avg'*100
	
	* (2) Only Quota 1 applicants
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optkv1_1==1 | optkv1_2==1) & $if"
	local opt ", robust"
	qui reg optaget2 over dist over_X_dist `if' `opt'
	local a = _b[over]
	local A = _se[over]^2
	qui reg restance over dist over_X_dist `if' `opt'
	local b = _b[over]
	local B = _se[over]^2
	local avg=_b[_cons]
	qui ivregress 2sls restance (optaget2 = over) dist over_X_dist `if' `opt'
	local c = _b[optaget2]
	local C = _se[optaget2]^2
	local N = e(N)

	mat b=J(1,3,.)
	mat V=J(3,3,0)
	mat b[1,1] = `a'
	mat b[1,2] = `b'
	mat b[1,3] = `c'
	mat V[1,1] = `A'
	mat V[2,2] = `B'
	mat V[3,3] = `C'
	mat colnames b = a b c
	mat colnames V = a b c
	mat rownames V = a b c

	ereturn post b V, depname(restance) obs(`N')
	times100
	eststo reg2
	qui estadd local run "Linear"
	qui estadd local bw "1.5"
	qui estadd local ker "Rect."
	qui estadd local non "\checkmark"
	qui estadd scalar bas=`avg'*100
	
	* (3) Only applicants who end up obtaining at least a Master's degree
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & (audd_niveau_10ya=="Lange videregående uddannelser" | audd_niveau_10ya=="Ph.d. og forskeruddannelser") & $if"
	local opt ", robust"
	qui reg optaget2 over dist over_X_dist `if' `opt'
	local a = _b[over]
	local A = _se[over]^2
	reg restance over dist over_X_dist `if' `opt'
	local b = _b[over]
	local B = _se[over]^2
	local avg=_b[_cons]
	qui ivregress 2sls restance (optaget2 = over) dist over_X_dist `if' `opt'
	local c = _b[optaget2]
	local C = _se[optaget2]^2
	local N = e(N)

	mat b=J(1,3,.)
	mat V=J(3,3,0)
	mat b[1,1] = `a'
	mat b[1,2] = `b'
	mat b[1,3] = `c'
	mat V[1,1] = `A'
	mat V[2,2] = `B'
	mat V[3,3] = `C'
	mat colnames b = a b c
	mat colnames V = a b c
	mat rownames V = a b c

	ereturn post b V, depname(restance) obs(`N')
	times100
	eststo reg3
	qui estadd local run "Linear"
	qui estadd local bw "1.5"
	qui estadd local ker "Rect."
	qui estadd local mas "\checkmark"
	qui estadd scalar bas=`avg'*100
	
	* (4) Only applicants who end up obtaining at least a Bachelor's degree
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & (audd_niveau_10ya=="Lange videregående uddannelser" | audd_niveau_10ya=="Ph.d. og forskeruddannelser" | audd_niveau_10ya=="Mellemlange videregående uddannelser / Bachelorer") & $if"
	local opt ", robust"
	qui reg optaget2 over dist over_X_dist `if' `opt'
	local a = _b[over]
	local A = _se[over]^2
	reg restance over dist over_X_dist `if' `opt'
	local b = _b[over]
	local B = _se[over]^2
	local avg=_b[_cons]
	qui ivregress 2sls restance (optaget2 = over) dist over_X_dist `if' `opt'
	local c = _b[optaget2]
	local C = _se[optaget2]^2
	local N = e(N)

	mat b=J(1,3,.)
	mat V=J(3,3,0)
	mat b[1,1] = `a'
	mat b[1,2] = `b'
	mat b[1,3] = `c'
	mat V[1,1] = `A'
	mat V[2,2] = `B'
	mat V[3,3] = `C'
	mat colnames b = a b c
	mat colnames V = a b c
	mat rownames V = a b c

	ereturn post b V, depname(restance) obs(`N')
	times100
	eststo reg4
	qui estadd local run "Linear"
	qui estadd local bw "1.5"
	qui estadd local ker "Rect."
	qui estadd local bac "\checkmark"
	qui estadd scalar bas=`avg'*100
		
	* (5) Using Chetty et al definition of economic programs
	local if "if abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & kot1_econ==0 & kot2_econ==1 & completed_fields==1"
	local opt ", robust"
	qui reg optaget2 over dist over_X_dist `if' `opt'
	local a = _b[over]
	local A = _se[over]^2
	qui reg restance over dist over_X_dist `if' `opt'
	local b = _b[over]
	local B = _se[over]^2
	local avg=_b[_cons]
	qui ivregress 2sls restance (optaget2 = over) dist over_X_dist `if' `opt'
	local c = _b[optaget2]
	local C = _se[optaget2]^2
	local N = e(N)

	mat b=J(1,3,.)
	mat V=J(3,3,0)
	mat b[1,1] = `a'
	mat b[1,2] = `b'
	mat b[1,3] = `c'
	mat V[1,1] = `A'
	mat V[2,2] = `B'
	mat V[3,3] = `C'
	mat colnames b = a b c
	mat colnames V = a b c
	mat rownames V = a b c

	ereturn post b V, depname(restance) obs(`N')
	times100
	eststo reg5
	qui estadd local run "Linear"
	qui estadd local bw "1.5"
	qui estadd local ker "Rect."
	qui estadd local che "\checkmark"
	qui estadd scalar bas=`avg'*100
	
	* (6) Excluding applicants who end up working in the finance and insurance sector
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & bra_finans_10ya==0 & $if"
	local opt ", robust"
	qui reg optaget2 over dist over_X_dist `if' `opt'
	local a = _b[over]
	local A = _se[over]^2
	reg restance over dist over_X_dist `if' `opt' // P-val 0.062
	local b = _b[over]
	local B = _se[over]^2
	local avg=_b[_cons]
	qui ivregress 2sls restance (optaget2 = over) dist over_X_dist `if' `opt'
	local c = _b[optaget2]
	local C = _se[optaget2]^2
	local N = e(N)

	mat b=J(1,3,.)
	mat V=J(3,3,0)
	mat b[1,1] = `a'
	mat b[1,2] = `b'
	mat b[1,3] = `c'
	mat V[1,1] = `A'
	mat V[2,2] = `B'
	mat V[3,3] = `C'
	mat colnames b = a b c
	mat colnames V = a b c
	mat rownames V = a b c

	ereturn post b V, depname(restance) obs(`N')
	times100
	eststo reg6
	qui estadd local run "Linear"
	qui estadd local bw "1.5"
	qui estadd local ker "Rect."
	qui estadd local fai "\checkmark"
	qui estadd scalar bas=`avg'*100
	
	* (7) Excluding applicants who are admitted to programs with many math peers
	cap drop math_peers
	gen 	math_peers = kot1_mat1 if optaget1==1
	replace math_peers = kot2_mat1 if optaget2==1
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & math_peers<0.5 & $if"
	local opt ", robust"
	qui reg optaget2 over dist over_X_dist `if' `opt'
	local a = _b[over]
	local A = _se[over]^2
	reg restance over dist over_X_dist `if' `opt' 
	local b = _b[over]
	local B = _se[over]^2
	local avg=_b[_cons]
	qui ivregress 2sls restance (optaget2 = over) dist over_X_dist `if' `opt'
	local c = _b[optaget2]
	local C = _se[optaget2]^2
	local N = e(N)

	mat b=J(1,3,.)
	mat V=J(3,3,0)
	mat b[1,1] = `a'
	mat b[1,2] = `b'
	mat b[1,3] = `c'
	mat V[1,1] = `A'
	mat V[2,2] = `B'
	mat V[3,3] = `C'
	mat colnames b = a b c
	mat colnames V = a b c
	mat rownames V = a b c

	ereturn post b V, depname(restance) obs(`N')
	times100
	eststo reg7
	qui estadd local run "Linear"
	qui estadd local bw "1.5"
	qui estadd local ker "Rect."
	qui estadd local fai "\checkmark"
	qui estadd scalar bas=`avg'*100
	
	esttab reg1 reg2 reg3 reg4 reg5 reg6 reg7 using "$tabs/TableA7.tex", replace ///
		b(1) se nonote star(* 0.1 ** 0.05 *** 0.01) /// 
		scalar("bas Baseline") ///
		mgroup("Default +10 years after", pattern(1 0 0 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
		coef(a "First stage" b "Reduced form" c "LATE") ///
		mtit("Incl. no and multiple field completers" "Quota 1" "Master's" "Bachelor's" "Chetty et al. definition" "Excl. bankers" "Excl. high math peers")

		
********************************************************************************

* FIGURE A4

	frame change binding_sample1
		
	cap drop restance_dif
	gen restance_dif = . 
	replace restance_dif = mean_restance_udd_yoa1 - mean_restance_udd_yoa2 if optaget1==1
	replace restance_dif = mean_restance_udd_yoa2 - mean_restance_udd_yoa1 if optaget2==1
	replace restance_dif = . if restance_dif==0

	local if "inrange(dist10,-5,5) & dist10!=0"
	cap drop bin
	xtile bin = restance_dif if `if', nq(10)
	
	local obs=29
	local dot "msym(O) mc(blue*.5)"
	
	cap postclose fig 
	postfile fig x y c using "$pofi/rd", replace
	levelsof bin, loc(b)
		foreach x in `b' {
		qui count if bin==`x' 
		if r(N)<=`obs' {
			di "X=`x': FOR FÅ OBSERVATIONER"  
		}
		if r(N)>`obs' {
			sum restance_dif if bin==`x', meanonly
			local avg = r(mean)
			reg restance if bin==`x' & `if'
			post fig (`avg') (_b[_cons]) (1)
		}
	}
	reg restance restance_dif if `if', robust
	local slope = round(_b[restance_dif],0.01)
	if r(table)[4,1] < 0.001 { // pvalue
		local sig "***"
	}
	post fig (-0.1) (_b[_cons]-0.1*_b[restance_dif]) (2)
	post fig (0.1)  (_b[_cons]+0.1*_b[restance_dif]) (2)
	postclose fig
		
	preserve
		use "$pofi/rd", clear
		twoway 	(scatter y x if c==1, `dot')  ///
				(line    y x if c==2, lp(solid) lc(gs5)) ///
				,  ///
					$layout ///
					ylabel(0.05(0.025)0.15, $lblobs)  ///
					xlabel(`xlbl', $lblobs) ///
					xtitle("Difference in default probability between detailed" "field of admission and counterfactual field") ytitle("Probability of default and delinquency") ///
					legend(off) ///
					text(0.11 -0.025 "Slope: 0`slope'`sig'") ///
					xsize(20) ysize(20) 
	restore
	graph export "$figs/FigureA4.pdf", replace	
	
	frame change binding_sample2

********************************************************************************
		
* TABLE A8

	local if "if f1==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	
	cap drop temp 
	gen temp = 1-over
	
	qui reg optaget1 temp dist over_X_dist `if' `opt'
		times100
		eststo reg1
		estadd scalar avg=_b[_cons]
	qui reg restance temp dist over_X_dist `if' `opt'
		times100
		eststo reg2
		estadd scalar avg=_b[_cons]
	qui reg restance temp dist over_X_dist i.f2 sex i.yoa i.aldersgrp `if' `opt'
		times100
		eststo reg3
		qui estadd local pre "\checkmark"
		qui estadd local con "\checkmark"
	qui reg restance temp dist over_X_dist i.f2 sex i.yoa i.aldersgrp i.inc_dum `if' `opt'
		times100
		eststo reg4
		qui estadd local pre "\checkmark"
		qui estadd local con "\checkmark"
		qui estadd local inc "\checkmark"
	qui ivregress 2sls restance (optaget1 = temp) dist over_X_dist  `if' `opt'
		times100
		eststo reg5

	esttab reg1 reg2 reg3 reg4 reg5 using "$tabs/TableA8.tex", replace ///
		b(1) se nonote keep(temp) ///
		scalar("avg Baseline" "pre Alt. field FE" "con Male, year \& age" "inc Income") star(* 0.1 ** 0.05 *** 0.01)  ///
		mgroup("B\&E (\%)" "Probability of default (\%)", pattern(1 1 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) end(\cmidrule(lr){2-2}\cmidrule(lr){3-6}))  ///
		rename(optaget1 temp) ///
		coef(temp "$\mathbbm{1}$(Economics)") ///
		mtit("1st stage" "Reduced form" "Reduced form" "Reduced form" "LATE")
		
* NUMBERS IN FOOTNOTE 12
	
	/*
		cap drop temp
		gen temp = (f1==f2) if !mi(f1) & !mi(f2) 
		tab f1 temp, row
		frame change admission_thresholds
		cap drop temp 
		gen temp=(adgangskvotient>0) if !mi(adgangskvotient)
		sum temp if kot_oeko==1 // 0.27
		sum temp if kot_sosc==1 // 0.77
		sum temp if kot_jura==1 // 1
		
		cap drop temp1
		gen temp1 = strpos(kot_navn, "økonomi")
		frame change binding_sample2
	*/
	
********************************************************************************
	
* TABLE A9

	* (1) Including partner's defaults
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	cap drop temp
	egen temp = rowmax(rest8_10_all rest8_10_all_efalle)
	replace temp = 0 if mi(temp)
	local opt ", robust"
	
	qui reg temp over dist over_X_dist `if' `opt'
		times100
		eststo reg1
		estadd scalar avg=_b[_cons]
	
	
	* (2) Drop defaults 2008-2010
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	
	qui reg restance_nfc over dist over_X_dist `if' `opt'
		times100
		eststo reg2
		estadd scalar avg=_b[_cons]
	
	* (3) Measuring default from 8 years after application
	cap drop restance_8_all
	gen 	restance_8_all = rest8_8_all
	replace restance_8_all = 0 if mi(restance_8_all) & in_irte_10_all==1 // Impute zeroes as in do 11
	
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	qui reg restance_8_all over dist over_X_dist `if' `opt'
		times100
		eststo reg3
		estadd scalar avg=_b[_cons]
	
	
	* (4) Measuring default from 12 years after application
	cap drop restance_12_all
	gen 	restance_12_all = rest8_12_all
	replace restance_12_all = 0 if mi(restance_12_all) & in_irte_10_all==1 & yoa<2005
	
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	qui reg restance_12_all over dist over_X_dist `if' `opt'
		times100
		eststo reg4
		estadd scalar avg=_b[_cons]
	
	* (5) Only defaults in 2016
	cap drop rest2016_0
	gen 	rest2016_0    = 0 
	replace	rest2016_0    = 1 if blb_rest8_2016>0    & !mi(blb_rest8_2016) 

	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"

	qui reg rest2016_0 over dist over_X_dist `if' `opt'
		times100
		eststo reg5
		estadd scalar avg=_b[_cons]

	* (6) Only defaults in 2016 with an outstanding amount of more than 2500 DKK
	cap drop rest2016_2500
	gen 	rest2016_2500 = 0
	replace	rest2016_2500 = 1 if blb_rest8_2016>2500 & !mi(blb_rest8_2016) 

	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	
	qui reg rest2016_2500 over dist over_X_dist `if' `opt'
		times100
		eststo reg6
		estadd scalar avg=_b[_cons]
		
	esttab reg1 reg2 reg3 reg4 reg5 reg6 using "$tabs/TableA9.tex", replace ///
		b(1) se nonote star(* 0.1 ** 0.05 *** 0.01) keep(over) /// 
		mgroup("Default +10 years after" "+8 years after" "+12 years after" "Default in 2016", pattern(1 0 1 1 1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) end(\cmidrule(lr){2-3}\cmidrule(lr){6-7})) ///
		coef(over "$\mathbbm{1}$(Economics)") ///
		mtit("Incl. partners' defaults" "Excl. 2008-10 defaults" " " " " "$> 0$ DKK" "$> 2500$ DKK")
		
********************************************************************************

* TABLE A10
	
	* (1), (2), (3), (4), and (5): Applicant per year observations
	preserve
		keep if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if
		keep pnr yoa dist dist10 over over_X_dist rest2_20* f1 f2
		
		reshape long rest2_, i(pnr yoa dist over f1 f2) j(year)
		rename rest2_ restance
		drop if year-yoa<10
		replace restance=0 if mi(restance)
		
		qui reg restance over dist over_X_dist, vce(cluster pnr)
			times100
			eststo reg1
			estadd scalar cluster=`e(N_clust)'
			estadd local level "\multirow{2}{*}{Applicant}"
			di _b[_cons]
		qui reg restance over dist over_X_dist i.yoa, vce(cluster pnr)
			times100
			eststo reg2
			estadd scalar cluster=`e(N_clust)'
			estadd local level "\multirow{2}{*}{Applicant}"
			estadd local yoa "\checkmark"
		qui reg restance over dist over_X_dist i.year, vce(cluster pnr)
			times100
			eststo reg3
			estadd scalar cluster=`e(N_clust)'
			estadd local level "\multirow{2}{*}{Applicant}"
			estadd local year "\checkmark"
		qui reg restance over dist over_X_dist i.yoa i.year, vce(cluster pnr)
			times100
			eststo reg4
			estadd scalar cluster=`e(N_clust)'
			estadd local level "\multirow{2}{*}{Applicant}"
			estadd local yoa "\checkmark"
			estadd local year "\checkmark"
		reg restance over dist over_X_dist i.yoa##i.year, vce(cluster pnr)
			times100
			eststo reg5
			estadd scalar cluster=`e(N_clust)'
			estadd local level "\multirow{2}{*}{Applicant}"
			estadd local yoa_year "\checkmark"	
	restore	
	
	* (6): Cluster by preferred field
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	
	tab kot1_field_l2 `if' 
	reg restance over dist over_X_dist `if' `opt'  cluster(kot1_field_l2)
		times100
		eststo reg6
		estadd scalar cluster=`e(N_clust)'
		estadd local level "Preferred field"
	
	* (7): Cluster by year of application using wild bootstrap
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	preserve
		keep `if'
		clustse reg restance over dist over_X_dist, cluster(yoa) method(wild) reps(1000)
	restore
		times100
		eststo reg7
		estadd scalar cluster=`e(N_yoa)'
		estadd local level "Year of application"
		* NOTE: No standard errors. Manually add a star and note that the P-val is 0.082
		
	* (8): Cluster by discrete values
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	
	reg restance over dist over_X_dist `if' `opt'  cluster(dist10)
		times100
		eststo reg8
		estadd scalar cluster=`e(N_clust)'
		estadd local level "Discrete running variable values"
		
	esttab reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 using "$tabs/TableA10.tex", replace ///
		b(2) se nonote nomtit keep(over) ///
		scalar("level \multirow{2}{*}{Cluster level}" "cluster Number of clusters" "yoa Year of application FE" "year Year of default FE" "yoa_year Year of app. $\times$ default FE") sfmt(g) star(* 0.1 ** 0.05 *** 0.01)  ///
		mgroup("Yearly probability of default (\%)" "Default +10 years after (\%)", pattern(1 0 0 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) end(\cmidrule(lr){2-6}\cmidrule(lr){7-9}))  ///
		coef(over "$\mathbbm{1}$(Economics)") 

********************************************************************************			

* FIGURE A5

	frame change binding_sample1
	
	* Panel A
	levelsof dist10, loc(d)
	cap postclose fig
	postfile fig dist10 antal using "$pofi/dens", replace
	foreach x in `d' {
		qui count if dist10==`x' & $if
		post fig (`x') (r(N))
	}
	postclose fig

	qui count if $if
	preserve
		use "$pofi/dens", clear
		gen dens = antal/r(N) if antal>29
		gen log_dens = log(dens)
		lpoly log_dens dist10 if dist10<0  [fweight=antal], deg(1) bw(6.8) kernel(tri) at(dist) gen(L) nogr
		lpoly log_dens dist10 if dist10>=0 [fweight=antal], deg(1) bw(6.8) kernel(tri) at(dist) gen(R) nogr
		twoway 	(scatter log_dens dist10 if inrange(dist10,-15,15), msym(Oh) mc(blue*.5)) ///
				(line    L dist10 if inrange(dist10,-15,0), lp(solid) lc(gs5)) ///
				(line    R dist10 if inrange(dist10,0,15),  lp(solid) lc(gs5)), ///
					xline(0, lp(dash) lc(black)) $layout legend(off) ///
					ylabel(-5.5(0.5)-2.5, $lblobs) yscale(range(-5.6 -2.5))  ///
					xlabel(-15 "-1.5" -10 "-1" -5 "-0.5" 0 "0" 5 "0.5" 10 "1" 15 "1.5", $lblobs) ///
					xtitle("Distance from threshold") ytitle("Log density") ///
					xsize(20) ysize(20) name(fig1, replace)
	restore
	
	* Panel B
	levelsof dist10, loc(d)
	cap postclose fig
	postfile fig dist10 antal using "$pofi/dens", replace
	foreach x in `d' {
		count if dist10==`x' & $if & f2==2 & f1!=1 & f2!=7
		post fig (`x') (r(N))
	}
	postclose fig

	count if $if & f2==2 & f1!=1 & f2!=7
	preserve
		use "$pofi/dens", clear
		gen dens = antal/r(N) if antal>29
		gen log_dens = log(dens)
		lpoly log_dens dist10 if dist10<0  [fweight=antal], deg(1) bw(8.5) kernel(tri) at(dist) gen(L) nogr
		lpoly log_dens dist10 if dist10>=0 [fweight=antal], deg(1) bw(8.5) kernel(tri) at(dist) gen(R) nogr
		twoway 	(scatter log_dens dist10 if inrange(dist10,-15,15), msym(Oh) mc(blue*.5)) ///
				(line    L dist10 if inrange(dist10,-15,0), lp(solid) lc(gs5)) ///
				(line    R dist10 if inrange(dist10,0,15),  lp(solid) lc(gs5)), ///
					xline(0, lp(dash) lc(black)) $layout legend(off) ///
					ylabel(-5.5(0.5)-2.5, $lblobs) yscale(range(-5.6 -2.5))  ///
					xlabel(-15 "-1.5" -10 "-1" -5 "-0.5" 0 "0" 5 "0.5" 10 "1" 15 "1.5", $lblobs) ///
					xtitle("Distance from threshold") ytitle("Log density") ///
					xsize(20) ysize(20) name(fig2, replace)
	restore
	
	graph export "$figs\FigureA5A.pdf", name(fig1) replace
	graph export "$figs\FigureA5B.pdf", name(fig2) replace
	graph close
	
********************************************************************************
	
* TABLE A11

	DCdensity dist10 if f1!=f2 & $if, breakpoint(0) generate(rd1 rd2 rd3 rd4 rd5) b(1) 
	cap drop rd1 rd2 rd3 rd4 rd5
	DCdensity dist10 if f1!=f2 & $if & f2==2 & f1!=1 & f2!=7, breakpoint(0) generate(rd1 rd2 rd3 rd4 rd5) b(1) 
	cap drop rd1 rd2 rd3 rd4 rd5
	graph close
	rddensity dist if f1!=f2 & $if
	rddensity dist if f1!=f2 & $if & f2==2 & f1!=1 & f2!=7
	
********************************************************************************
	
* TABLE A12
	
	frame change binding_sample2

	cap drop vu_far
	cap drop vu_mor
	gen vu_far=(audd_niveau_far=="Lange videregående uddannelser" | audd_niveau_far=="Ph.d. og forskeruddannelser") if !mi(audd_niveau_far)
	gen vu_mor=(audd_niveau_mor=="Lange videregående uddannelser" | audd_niveau_mor=="Ph.d. og forskeruddannelser") if !mi(audd_niveau_mor)
	
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	
	qui reg sex over dist over_X_dist `if' `opt'
		times100
		eststo reg1
	qui reg gpa over dist over_X_dist `if' `opt'
		eststo reg2
	qui reg yoa over dist over_X_dist `if' `opt'
		eststo reg3
	qui reg alder_yoa over dist over_X_dist `if' `opt'
		eststo reg4
	qui reg inc_rank_45_far over dist over_X_dist `if' `opt'
		eststo reg5
	qui reg inc_rank_45_mor over dist over_X_dist `if' `opt'
		eststo reg6
	qui reg vu_far over dist over_X_dist `if' `opt'
		times100
		eststo reg7
	qui reg vu_mor over dist over_X_dist `if' `opt'
		times100
		eststo reg8
	forval x=1/14 {
		qui reg yoa`x' over dist over_X_dist `if' `opt'
			times100
			eststo reg1`x'
	}
	forval x=1/5 {
		qui reg alder_yoa_`x' over dist over_X_dist `if' `opt'
			times100
			eststo reg2`x'
	}
	cap drop f1_dum_*
	qui tab f1 `if', gen(f1_dum_)
	forval x=1/6 {
		qui reg f1_dum_`x' dist over over_X_dist `if' `opt'
			times100
			eststo reg3`x'
	}
	cap drop f1_dum_*
	
	esttab reg1 reg2 reg24 reg25, se
	qui esttab reg1 reg2 reg5 reg6 reg7 reg8 reg3 reg11 reg12 reg13 reg14 reg15 reg16 reg17 reg18 reg19 reg110 reg111 reg112 reg113 reg114 reg4 reg21 reg22 reg23 reg24 reg25 reg31 reg32 reg33 reg34 reg35 reg36, se
	
	mat b = J(1,33,.)
	mat rownames b = over
	mat colnames b = reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 reg9 reg10 reg11 reg12 reg13 reg14 reg15 reg16 reg17 reg18 reg19 reg20 reg21 reg22 reg23 reg24 reg25 reg26 reg27 reg28 reg29 reg30 reg31 reg32 reg33
	mat V = J(33,33,1)
	mat rownames V = reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 reg9 reg10 reg11 reg12 reg13 reg14 reg15 reg16 reg17 reg18 reg19 reg20 reg21 reg22 reg23 reg24 reg25 reg26 reg27 reg28 reg29 reg30 reg31 reg32 reg33
	mat colnames V = reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 reg9 reg10 reg11 reg12 reg13 reg14 reg15 reg16 reg17 reg18 reg19 reg20 reg21 reg22 reg23 reg24 reg25 reg26 reg27 reg28 reg29 reg30 reg31 reg32 reg33
	mat N = J(1,33,.)
	mat rownames N = obs
	mat colnames N = reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 reg9 reg10 reg11 reg12 reg13 reg14 reg15 reg16 reg17 reg18 reg19 reg20 reg21 reg22 reg23 reg24 reg25 reg26 reg27 reg28 reg29 reg30 reg31 reg32 reg33
	
	forval x=1/33 {
		di "`x'"
		matrix b[1,`x']=r(coefs)[1,(`x'-1)*3+1]
		matrix V[`x',`x']=(r(coefs)[1,(`x'-1)*3+2])^2
		matrix N[1,`x']=r(stats)[1,`x']
	}
	
	eststo clear
	ereturn post b V
	eststo col1
	mat b = N
	ereturn post b
	eststo col2

	esttab col1 col2 using "$tabs/TableA12.tex", replace ///
		b(a2) noobs se nonote wide nogaps star(* 0.1 ** 0.05 *** 0.01) nonum ///
		mtitle("$\mathbbm{1}$(Economics)" "\textit{N}") ///
		refcat(reg28 "Preferred field", nolabel) ///
		coef( 								///
			reg1  "Male (\%)"              	///
			reg2  "GPA"						///
			reg3  "Inc. rank father"      	///
			reg4  "Inc. rank mother"		///
			reg5  "Higher edu. father (\%)"	///
			reg6  "Higher edu. mother (\%)"	///
			reg7  "Year of application"		///
			reg8  "\hspace{1cm}1993 (\%)"	///
			reg9  "\hspace{1cm}1994 (\%)"	///
			reg10 "\hspace{1cm}1995 (\%)"	///
			reg11 "\hspace{1cm}1996 (\%)"	///
			reg12 "\hspace{1cm}1997 (\%)"	///
			reg13 "\hspace{1cm}1998 (\%)"	///
			reg14 "\hspace{1cm}1999 (\%)"	///
			reg15 "\hspace{1cm}2000 (\%)"	///
			reg16 "\hspace{1cm}2001 (\%)"	///
			reg17 "\hspace{1cm}2002 (\%)"	///
			reg18 "\hspace{1cm}2003 (\%)"	///
			reg19 "\hspace{1cm}2004 (\%)"	///
			reg20 "\hspace{1cm}2005 (\%)"	///
			reg21 "\hspace{1cm}2006 (\%)"	///
			reg22 "Age at application"		///
			reg23 "\hspace{1cm} 18-19 (\%)"	///
			reg24 "\hspace{1cm} 20-21 (\%)"	///
			reg25 "\hspace{1cm} 22-23 (\%)"	///
			reg26 "\hspace{1cm} 24-25 (\%)"	///
			reg27 "\hspace{1cm} 25-30 (\%)" ///
			reg28 "\hspace{1cm} Law (\%)" ///
			reg29 "\hspace{1cm} Hum., Art \& Arch. (\%)" ///
			reg30 "\hspace{1cm} Education (\%)" ///
			reg31 "\hspace{1cm} Welfare (\%)" ///
			reg32 "\hspace{1cm} Medicine (\%)" ///
			reg33 "\hspace{1cm} Social Science (\%)" ///
		)
		
********************************************************************************

* FIGURE A6
	
	* Panel A
	cap drop postfile robust
	postfile robust x y h l using "$pofi/robust", replace
	forval x=1993/2006 {
		qui reg restance over dist over_X_dist if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if & yoa!=`x', robust
		post robust (`x') (_b[over]) (_b[over]+1.65*_se[over]) (_b[over]-1.65*_se[over])
	}
	qui reg restance over dist over_X_dist if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if, robust
	local b = _b[over]
	local h = _b[over]+1.65*_se[over]
	local l = _b[over]-1.65*_se[over]
	postclose robust
	
	preserve 
		use "$pofi/robust", clear
		gen b = `b'
		gen b_h = `h'
		gen b_l = `l'
		twoway	(line b x,    lp(solid) color(blue*0.5)) ///
				(line b_h x,  lp(dash)  color(blue*0.5)) ///
				(line b_l x,  lp(dash)  color(blue*0.5)) ///
				(line y x,    lp(solid) color(gs5)) ///
				(rarea h l x, color(gs5%20)), ///
					$layout xsize(20) ysize(20) ///
					xlabel(1993(2)2006, $lblobs) ylabel(-.12(.02)0,$lblobs) ///
					legend(order(4 5 1 2) ring(0) pos(7) r(2) label(4 "Estimate") label(5 "90% CI") label(1 "Baseline") label(2 "90% CI")) ///
				xtitle("Year of application cohort left out of estimation") ytitle("Effect of admission to economics on default") ///
				name(fig1, replace)
	restore
	
	* Panel B
	cap drop postfile robust
	postfile robust x y h l using "$pofi/robust", replace
	forval x=3/8 {
		qui reg restance over dist over_X_dist if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if & f1!=`x', robust
		post robust (`x') (_b[over]) (_b[over]+1.65*_se[over]) (_b[over]-1.65*_se[over])
	}
	qui reg restance over dist over_X_dist if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if, robust
	local b = _b[over]
	local h = _b[over]+1.65*_se[over]
	local l = _b[over]-1.65*_se[over]
	postclose robust
	
	preserve 
		use "$pofi/robust", clear
		gen b = `b'
		gen b_h = `h'
		gen b_l = `l'
		twoway	(line b x,    lp(solid) color(blue*0.5)) ///
				(line b_h x,  lp(dash)  color(blue*0.5)) ///
				(line b_l x,  lp(dash)  color(blue*0.5)) ///
				(scatter y x,    lp(solid) color(gs5)) ///
				(rcap h l x, color(gs5)), ///
					$layout xsize(20) ysize(20) ///
					xlabel(3 "Law" 4 "HAA" 5 "Education" 6 "Welfare" 7 "Medicine" 8 "SocSci", $lblobs) ylabel(-.12(.02)0,$lblobs) ///
					legend(order(4 5 1 2) ring(0) pos(5) r(2) label(4 "Estimate") label(5 "90% CI") label(1 "Baseline") label(2 "90% CI")) ///
				xtitle("Preferred field of study left out of estimation") ytitle("Effect of admission to economics on default") ///
				name(fig2, replace)
	restore
	
	graph export "$figs\FigureA6A.pdf",  name(fig1) replace
	graph export "$figs\FigureA6B.pdf", name(fig2) replace
	graph close

********************************************************************************

* Figure A7
	
	local if "if f2==2 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"

	cap postclose bw_plot
	postfile bw_plot x b h l using "$pofi/bw_plot", replace
	forval x=1/30 {
		local if "if f2==2 & abs(dist10)<=`x' & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
		reg restance over dist over_X_dist `if'
		post bw_plot (`x') (_b[over]) (_b[over]+1.65*_se[over]) (_b[over]-1.65*_se[over])
	}
	postclose bw_plot

	preserve 
		use "$pofi/bw_plot", clear
		sum l if x==15, meanonly
		gen l_ci = r(mean)
		sum h if x==15, meanonly
		gen h_ci = r(mean)
		twoway 	(line b x, lp(solid) color(gs5) ) ///
				(line h_ci x, lc(black) lp(dash)) ///
				(line l_ci x, lc(black) lp(dash)) ///
				(rarea h l x, color(gs5%20)), ///
				$layout xsize(20) ysize(20) xscale(reverse) ///
				legend(order(1 4 2) ring(0) pos(7) r(3) label(1 "Point estimate") label(4 "90% CI") label(2 "90% CI at 1.5")) ///
				xtitle("Running variable bandwidth in estimation") ytitle("Effect of admission to economics on default") ///
				xlabel(30 "3" 25 "2.5" 20 "2" 15 "1.5" 10 "1" 5 "0.5", $lblobs) ylabel(-.2(.05).1,$lblobs)
				
	restore
		graph export "$figs\FigureA7.pdf", replace
		graph close
		
********************************************************************************

* TABLE A13

	* (1)
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	qui reg optaget2 over dist over_X_dist `if' `opt'
	local a = _b[over]
	local A = _se[over]^2
	qui reg restance over dist over_X_dist `if' `opt'
	local b = _b[over]
	local B = _se[over]^2
	local avg=_b[_cons]
	qui ivregress 2sls restance (optaget2 = over) dist over_X_dist `if' `opt'
	local c = _b[optaget2]
	local C = _se[optaget2]^2
	local N = e(N)

	mat b=J(1,3,.)
	mat V=J(3,3,0)
	mat b[1,1] = `a'
	mat b[1,2] = `b'
	mat b[1,3] = `c'
	mat V[1,1] = `A'
	mat V[2,2] = `B'
	mat V[3,3] = `C'
	mat colnames b = a b c
	mat colnames V = a b c
	mat rownames V = a b c

	ereturn post b V, depname(restance) obs(`N')
	times100
	eststo reg1
	qui estadd scalar com=`avg'*100
	qui estadd local run "Linear"
	qui estadd scalar bw = 1.5
	qui estadd local ker "Rect."

	* (2)
	cap drop weight
	local bw = 15
	gen 	weight = max(0,`bw'-abs(dist10)) 

	local if "if f2==2 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	reg optaget2 over dist over_X_dist `if' [pw=weight] `opt'
	local a = _b[over]
	local A = _se[over]^2
	reg restance over dist over_X_dist `if' [pw=weight] `opt'
	local b = _b[over]
	local B = _se[over]^2
	local avg=_b[_cons]
	ivregress 2sls restance (optaget2 = over) dist over_X_dist `if' [pw=weight] `opt'
	local c = _b[optaget2]
	local C = _se[optaget2]^2
	local N = e(N)

	mat b=J(1,3,.)
	mat V=J(3,3,0)
	mat b[1,1] = `a'
	mat b[1,2] = `b'
	mat b[1,3] = `c'
	mat V[1,1] = `A'
	mat V[2,2] = `B'
	mat V[3,3] = `C'
	mat colnames b = a b c
	mat colnames V = a b c
	mat rownames V = a b c

	ereturn post b V, depname(restance) obs(`N')
	times100
	eststo reg2
	qui estadd scalar com=`avg'*100
	qui estadd local run "Linear"
	qui estadd scalar bw = 1.5
	qui estadd local ker "Tri."
	
	* (3)
	cap drop dist_sq 
	gen dist_sq = dist*dist
	cap drop over_X_dist_sq
	gen over_X_dist_sq = over*dist_sq 
	local if "if f2==2 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	qui reg optaget2 over dist dist_sq over_X_dist over_X_dist_sq `if' `opt'
	local a = _b[over]
	local A = _se[over]^2
	qui reg restance over dist dist_sq over_X_dist over_X_dist_sq `if' `opt'
	local b = _b[over]
	local B = _se[over]^2
	local avg=_b[_cons]
	qui ivregress 2sls restance (optaget2 = over) dist dist_sq over_X_dist over_X_dist_sq `if' `opt'
	local c = _b[optaget2]
	local C = _se[optaget2]^2
	local N = e(N)

	mat b=J(1,3,.)
	mat V=J(3,3,0)
	mat b[1,1] = `a'
	mat b[1,2] = `b'
	mat b[1,3] = `c'
	mat V[1,1] = `A'
	mat V[2,2] = `B'
	mat V[3,3] = `C'
	mat colnames b = a b c
	mat colnames V = a b c
	mat rownames V = a b c

	ereturn post b V, depname(restance) obs(`N')
	times100
	eststo reg3
	qui estadd scalar com=`avg'*100
	qui estadd local run "Quadratic"
	qui estadd local ker "Rect."
	
	* (4)
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & dist!=0 & $if"
	local opt ", robust"
	qui reg optaget2 over dist over_X_dist `if' `opt'
	local a = _b[over]
	local A = _se[over]^2
	qui reg restance over dist over_X_dist `if' `opt'
	local b = _b[over]
	local B = _se[over]^2
	local avg=_b[_cons]
	qui ivregress 2sls restance (optaget2 = over) dist over_X_dist `if' `opt'
	local c = _b[optaget2]
	local C = _se[optaget2]^2
	
	local N = e(N)

	mat b=J(1,3,.)
	mat V=J(3,3,0)
	mat b[1,1] = `a'
	mat b[1,2] = `b'
	mat b[1,3] = `c'
	mat V[1,1] = `A'
	mat V[2,2] = `B'
	mat V[3,3] = `C'
	mat colnames b = a b c
	mat colnames V = a b c
	mat rownames V = a b c

	ereturn post b V, depname(restance) obs(`N')
	times100
	eststo reg4
	qui estadd scalar com=`avg'*100
	qui estadd local run "Linear"
	qui estadd scalar bw = 1.5
	qui estadd local ker "Rect."
	qui estadd local donut "\checkmark"	
	
	* (5)
	local if "if f2==2 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & dist!=0 & $if"
	
	qui ikbw restance dist10 `if'
	
	cap drop weight
	local bw = e(hwid)
	gen 	weight = max(0,`bw'-abs(dist10)) 

	local opt ", robust"
	reg optaget2 over dist over_X_dist `if' [pw=weight] `opt'
	local a = _b[over]
	local A = _se[over]^2
	reg restance over dist over_X_dist `if' [pw=weight] `opt'
	local b = _b[over]
	local B = _se[over]^2
	local avg=_b[_cons]
	ivregress 2sls restance (optaget2 = over) dist over_X_dist `if' [pw=weight] `opt'
	local c = _b[optaget2]
	local C = _se[optaget2]^2
	local N = e(N)

	mat b=J(1,3,.)
	mat V=J(3,3,0)
	mat b[1,1] = `a'
	mat b[1,2] = `b'
	mat b[1,3] = `c'
	mat V[1,1] = `A'
	mat V[2,2] = `B'
	mat V[3,3] = `C'
	mat colnames b = a b c
	mat colnames V = a b c
	mat rownames V = a b c

	ereturn post b V, depname(restance) obs(`N')
	times100
	eststo reg5
	qui estadd scalar com=`avg'*100
	qui estadd local run "Linear"
	qui estadd scalar bw = floor(`bw')/10
	qui estadd local ker "Tri."
	qui estadd local donut "\checkmark"	
	
	* (6) 
	cap drop temp 
	gen temp = -dist
	local if "if f2==2 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & dist!=0 & $if"
	qui rdrobust optaget2 temp `if', 
	local a = e(b)[1,1]
	local A = e(V)[1,1]
	qui rdrobust restance temp `if', 
	local b = e(b)[1,1]
	local B = e(V)[1,1]
	local avg=e(beta_p_l)[1,1]
	rdrobust restance temp `if', fuzzy(optaget2) 
	local c = e(b)[1,1]
	local C = e(V)[1,1]
	local N = e(N_h_l)+e(N_h_r)
	
	mat b=J(1,3,.)
	mat V=J(3,3,0)
	mat b[1,1] = `a'
	mat b[1,2] = `b'
	mat b[1,3] = `c'
	mat V[1,1] = `A'
	mat V[2,2] = `B'
	mat V[3,3] = `C'
	mat colnames b = a b c
	mat colnames V = a b c
	mat rownames V = a b c
	
	ereturn post b V, depname(restance) obs(`N')
	times100
	eststo reg6
	qui estadd scalar com=`avg'*100
	qui estadd local run "Linear"
	qui estadd local bw "0.6"
	qui estadd local ker "Tri."
	qui estadd local donut "\checkmark"	
	
	* (7)
	local if "if f2==2 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & dist10!=0 & $if & (dist10==-1 | dist10==1)"
	local opt ", robust"
	qui reg optaget2 over `if' `opt'
	local a = _b[over]
	local A = _se[over]^2
	qui reg restance over `if' `opt'
	local b = _b[over]
	local B = _se[over]^2
	local avg=_b[_cons]
	qui ivregress 2sls restance (optaget2 = over) `if' `opt'
	local c = _b[optaget2]
	local C = _se[optaget2]^2
	local N = e(N)
	
	mat b=J(1,3,.)
	mat V=J(3,3,0)
	mat b[1,1] = `a'
	mat b[1,2] = `b'
	mat b[1,3] = `c'
	mat V[1,1] = `A'
	mat V[2,2] = `B'
	mat V[3,3] = `C'
	mat colnames b = a b c
	mat colnames V = a b c
	mat rownames V = a b c
	
	ereturn post b V, depname(restance) obs(`N')
	times100
	eststo reg7
	qui estadd scalar com=`avg'*100
	qui estadd local run "-"
	qui estadd local bw "0.1"
	qui estadd local ker "-"
	qui estadd local donut "\checkmark"	
	
	esttab reg1 reg2 reg3 reg4 reg5 reg6 reg7 using "$tabs/TableA13.tex", replace ///
		b(1) se nomtit nonote ///
		scalar("com Baseline" "bw Bandwidth" "run Functional form" "ker Kernel" "donut Donut RD") star(* 0.1 ** 0.05 *** 0.01)  ///
		mgroup("Probability of default more than 10 years after application", pattern(1 0 0 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(})) coef(a "First stage" b "Reduced form" c "LATE")

********************************************************************************	

* FIGURE A8

	forval x = 1/10 {
		cap drop dist10_m`x'
		gen dist10_m`x' = dist10+`x'
		cap drop over_m`x'
		gen over_m`x' = (dist10_m`x'<0)
	}

	forval x = 1/10 {
		cap drop dist10_p`x'
		gen dist10_p`x' = dist10-`x'
		cap drop over_p`x'
		gen over_p`x' = (dist10_p`x'<0)
	}
	
	local if "f2==2  & (optaget1==1 | optaget2==1) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & $if"
	local opt ", robust"
	local ci = 1.65
	foreach b in 5 9 10 15 20 30 {
		cap postclose coef_plot
		postfile coef_plot x b h l using "$pofi/coef_plot", replace
		loc bw = `b'
		local i = -6
		foreach x in m5 m4 m3 m2 m1 p1 p2 p3 p4 p5 p6 {
			local i = `i'+1
			qui reg restance i.over_`x'##c.dist10_`x' if `if' & inrange(dist10_`x',-`bw',`bw') `opt'
			if `i'!=0 {
				post coef_plot (`i') (_b[1.over_`x']) (_b[1.over_`x']+`ci'*_se[1.over_`x']) (_b[1.over_`x']-`ci'*_se[1.over_`x'])
			}
			di "N=`e(N)'"
		}
		qui reg restance i.over##c.dist10                 if `if' & inrange(dist10    ,-`bw',`bw') `opt'
		post coef_plot (0) (_b[1.over]) (_b[1.over]+`ci'*_se[1.over]) (_b[1.over]-`ci'*_se[1.over])
		di "N=`e(N)'"
		cap postclose coef_plot

		preserve
			use "$pofi/coef_plot", clear
			twoway 	(bar b x if x!=0, horizontal col(blue*.5) )  ///
					(bar b x if x==0, horizontal col(red*.5) )  ///
					(rcap h l x, horizontal lc(gs5)), ///
					$layout xsize(20) ysize(20) ///
					legend(off) ///
					ylabel(-5 "-0.5" 0 "0" 5 "0.5", labsize(small)) ///
					xlabel(-0.1(0.05)0.1, labsize(small)) xscale(range(-0.11 0.11)) ///
					ytitle("Threshold") xtitle("Estimated effect of being above threshold") ///
					name(fig_`b', replace)
		restore 
	}
	graph export "$figs\FigureA8A.pdf", replace name(fig_5)
	graph export "$figs\FigureA8B.pdf", replace name(fig_10)
	graph export "$figs\FigureA8C.pdf", replace name(fig_9)
	graph export "$figs\FigureA8D.pdf", replace name(fig_15)
	graph export "$figs\FigureA8E.pdf", replace name(fig_20)
	graph export "$figs\FigureA8F.pdf", replace name(fig_30)
	
	graph close
	
	reg restance i.over##c.dist10 if f2==2  & (optaget1==1 | optaget2==1) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & $if & inrange(dist10,-9,9), robust // P-val 0.055
	
********************************************************************************

* FIGURE 4

	local if "abs(dist10)<=15 & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	local outcome "restance"
	local ci=1.65

	cap postclose  coef_plot
	postfile coef_plot x b h l using "$pofi/coef_plot", replace
	foreach x in 1 2 3 4 5 6 7 8 {
		cap drop d`x'
		gen 	d`x'=0
		replace d`x'=1 if f1==`x' & over==0
		replace d`x'=1 if f2==`x' & over==1
		cap drop fst`x'
		gen fst`x' = (f1==`x')
		
		reg `outcome' d`x' c.dist##i.fst`x'##i.over if `if' & (f2==`x' | f1==`x') `opt'
		post coef_plot (`x') (_b[d`x']) (_b[d`x']+`ci'*_se[d`x']) (_b[d`x']-`ci'*_se[d`x'])
	}
	postclose  coef_plot

	preserve
		use "$pofi/coef_plot", clear
		replace x = -8 if x==1
		replace x = -7 if x==7
		replace x = -6 if x==2
		replace x = -5 if x==3
		replace x = -4 if x==4
		replace x = -3 if x==5
		replace x = -2 if x==6
		replace x = -1 if x==8
		twoway 	(bar b x, horizontal col(blue*.5) barw(.8))  ///
				(rcap h l x, horizontal lc(gs5)), ///
				$layout xsize(20) ysize(10) ///
				legend(r(2) ring(0) pos(11) label(1 "Effect") label(2 "90% CI") size(small)) ///
				ylabel(-8 "STEM" -6 "Bus. & econ." -5 "Law" -4 "H, A & A" -3 "Education" -2 "Welfare" -7 "Medicine" -1 "Soc. sci.", angle(360)) ///
				xlabel(-0.05(0.05)0.05, labsize(small)) ///
				ytitle("") xtitle("Effect on the default probability of admission to the field") 
	restore 
	
	graph export  "$figs\Figure4.pdf", replace
	graph close		
	
********************************************************************************	

* FIGURE 5
	
	local if "if f2==2 & (optaget1==1 | optaget2==1) & !mi(restance) & $if"

	foreach var in at_vals L1 L2 L3 L4 R1 R2 R3 R4 {
		cap drop `var'
	}
	gen at_vals = _n-21 if _n<42
	lpoly restance dist10 `if' & dist10<0  & f1==3, deg(1) bw(15) kernel(rec) at(at_vals) gen(L2) nogr
	lpoly restance dist10 `if' & dist10>=0 & f1==3, deg(1) bw(15) kernel(rec) at(at_vals) gen(R2) nogr
	lpoly restance dist10 `if' & dist10<0  & f1==8, deg(1) bw(15) kernel(rec) at(at_vals) gen(L4) nogr
	lpoly restance dist10 `if' & dist10>=0 & f1==8, deg(1) bw(15) kernel(rec) at(at_vals) gen(R4) nogr
	cap postclose fig
	postfile fig x y str5 grph using "$pofi/rd", replace

	forval x = -15/15 {
		sum L2 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L2") 
		sum R2 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("R2") 
		sum L4 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L4") 
		sum R4 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("R4") 
	}
	postclose fig
	drop L* R* at_vals

	preserve
		use "$pofi/rd", clear
		replace y=. if y<0
		twoway 	(line    y x if grph=="L2" & x<=0, lw(medthick) lp(solid) lc(green*.5)) ///
				(line    y x if grph=="R2" & x>=0, lw(medthick) lp(solid) lc(green*.5)) ///
				(line    y x if grph=="L4" & x<=0, lw(medthick) lp(solid) lc(red*.5)) ///
				(line    y x if grph=="R4" & x>=0, lw(medthick) lp(solid) lc(red*.5)) ///
				,  ///
					xline(0, lp(dash) lc(black)) $layout ///
					ylabel(0(.05).2, $lblobs)  ///
					xlabel(-15 "-1.5" -10 "-1" -5 "-0.5" 0 "0" 5 "0.5" 10 "1" 15 "1.5", $lblobs) ///
					xtitle("Distance from threshold") ytitle("Share of applicants in default") ///
					legend(order(1 3) ring(0) pos(2) r(3) size(small) symx(*.5) label(1 Law) label(3 Soc. science) title(Preferred field, size(medsmall))) ///
					xsize(20) ysize(11)
	restore
	
	graph export  "$figs\Figure5.pdf", replace			

********************************************************************************
	
* TABLE A14
	
	local if "f2==2 & abs(dist10)<=15 & !mi(restance) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	qui reg optaget2 over dist over_X_dist if `if' & f1==3 `opt'
		times100
		eststo reg1
	qui reg restance over dist over_X_dist if `if' & f1==3 `opt'
		times100
		eststo reg2
	qui ivregress 2sls restance (optaget2 = over) dist over_X_dist if `if' & f1==3 `opt'
		times100
		eststo reg3
	qui reg optaget2 over dist over_X_dist if `if' & f1==8 `opt'
		times100
		eststo reg4
	qui reg restance over dist over_X_dist if `if' & f1==8 `opt'
		times100
		eststo reg5
	qui ivregress 2sls restance (optaget2 = over) dist over_X_dist if `if' & f1==8 `opt'
		times100
		eststo reg6
	
	esttab reg1 reg2 reg3 reg4 reg5 reg6 using "$tabs/TableA14.tex", replace  ///
		b(1) nonum nomtit se star(* 0.1 ** 0.05 *** 0.01) nonote keep(over)  ///
		rename(optaget2 over) coef(over "$\mathbbm{1}$(Economics)")  ///
		mgroup("Law" "Social Science", pattern(1 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) end(\cmidrule(lr){2-4}\cmidrule(lr){5-7})) ///
		posthead(&\multicolumn{1}{c}{B\&E (\%)}&\multicolumn{2}{c}{Default (\%)}&\multicolumn{1}{c}{B\&E (\%)}&\multicolumn{2}{c}{Default (\%)}\\ \cmidrule(lr){2-2}\cmidrule(lr){3-4}\cmidrule(lr){5-5}\cmidrule(lr){6-7} &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}&\multicolumn{1}{c}{(5)}&\multicolumn{1}{c}{(6)}\\ &\multicolumn{1}{c}{1st stage}&\multicolumn{1}{c}{Reduced form}&\multicolumn{1}{c}{LATE}&\multicolumn{1}{c}{1st stage}&\multicolumn{1}{c}{Reduced form}&\multicolumn{1}{c}{LATE}\\ \hline)
	
	sum	optkv2_1 if f2==2 & f1==3 & abs(dist10)<=15 & !mi(restance) & (optaget1==1 | optaget2==1) & $if
	sum	optkv2_1 if f2==2 & f1==8 & abs(dist10)<=15 & !mi(restance) & (optaget1==1 | optaget2==1) & $if
	
	
********************************************************************************

* TABLE A15

	tab kot1_field_l2 if kot2_field_l2=="Economics"
	
	local if "if abs(dist10)<=20 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & completed_fields==1"
	local opt ", robust"

	cap drop temp 
	gen temp = . 
	replace temp = 1 if kot2_field_l2=="Economics" & kot1_field_l2=="Political sciences and civics"
	replace temp = 2 if kot2_field_l2=="Economics" & kot1_field_l2=="Law"
	replace temp = 3 if kot2_field_l2=="Economics" & kot1_field_l2=="Business and administration not further defined"
	replace temp = 4 if kot2_field_l2=="Economics" & kot1_field_l2=="Inter-disciplinary programmes and qualifications involving business and administration"
	tab temp
	
	qui reg restance over dist over_X_dist `if' & temp==1 `opt'
		times100
		eststo reg1
		estadd scalar avg=_b[_cons]
	qui reg restance over dist over_X_dist `if' & temp==2 `opt'
		times100
		eststo reg2
		estadd scalar avg=_b[_cons]
	qui reg restance over dist over_X_dist `if' & temp==3 `opt'
		times100
		eststo reg3
		estadd scalar avg=_b[_cons]
	qui reg restance over dist over_X_dist `if' & temp==4 `opt'
		times100
		eststo reg4
		estadd scalar avg=_b[_cons]
	
	
	esttab reg1 reg2 reg3 reg4 using "$tabs/TableA15.tex", replace ///
		b(1) se nonote keep(over) nonum ///
		scalar("avg Baseline") star(* 0.1 ** 0.05 *** 0.01)  ///
		coef(over "$\mathbbm{1}$(Economics)") ///
		mgroup("Probability of default (\%)", pattern(1 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) end(\cmidrule(lr){2-5}))  ///
		mtit("Political science" "Law" "Business econ." "Inter-disciplinary business econ.")
	

********************************************************************************
	
* FIGURE A9

	local if "if abs(dist10)<=15 & (optaget1==1 | optaget2==1) & !mi(restance) & $if"
	
	gen grp=.
	forval y = 1/8 {
		forval x=1/8 {
			if `y'!=`x' & `y'!=1 & `x'!=7 {
				replace grp=`y'`x' if f1==`y' & f2==`x'
			}
		}
	}

	cap postclose f_stats
	postfile f_stats grp F N using "$pofi\F_stats", replace
	levelsof grp, loc(g)
	foreach x in `g' {
		qui reg optaget1 i.over##c.dist `if' & grp==`x' 
		post f_stats (`x') (e(F)) (e(N))	
	}
	postclose f_stats

	preserve 
		use "$pofi\F_stats", clear
		drop if grp==25 | grp==26 | grp==53 | grp==75 
		replace F=205 if F>200
		hist F if N>25, width(10) start(0) freq $layout xsize(20) ysize(20) fcol(blue*.5) lcol(blue*.6) ///
			xtitle(F-statistic from own instrument regressions) ///
			ylabel(0(3)12, $lblobs) ///
			xlabel(0(50)200, $lblobs)
	restore
	
	graph export "$figs\FigureA9.pdf", replace
	graph close
	
********************************************************************************	

* TABLE A16 AND A17
	
	cap drop over_X_grp
	gen over_X_grp = over*grp
	cap drop over_X_f1
	gen over_X_f1 = over*f1
	cap drop opt2_X_f1
	gen opt2_X_f1 = optaget2*f1
	cap drop opt_X_grp
	gen opt_X_grp = optaget2*grp 
	
	local names1 = "2 3 4 6 7 8"
	local names2 = "3 4 6 7 8"
	local names3 = "2 4 6 7 8"
	local names4 = "2 3 5 6 7 8"
	local names5 = "3 4 6 8"
	local names6 = "3 4 5 7 8"
	local names8 = "2 3 4 6 7" 

	local if "if abs(dist10)<=15 & (optaget1==1 | optaget2==1) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & (grp!=25 & grp!=26 & grp!=53 & grp!=75 & grp!=51 & grp!=58 & grp!=52) & !mi(restance) & !mi(inc_dum) & $if"
	count `if'

	* A16
	local j=0
	foreach x in 1 2 3 4 5 6 8 {
		loc ++j
		di "ALTERNATIVE FIELD: `x'"
		qui reg restance i.over_X_grp i.f2##c.dist i.f1##c.over_X_dist i.grp  `if'
		if `x'==4 | `x'==1  {
			mat b=J(1,6,.)
			mat V=J(6,6,0)
		}
		if `x'==2 | `x'==3 | `x'==6 | `x'==8 {
			mat b=J(1,5,.)
			mat V=J(5,5,0)
		}
		if `x'==5  {
			mat b=J(1,4,.)
			mat V=J(4,4,0)
		}
		mat colnames b = `names`x''
		mat colnames V = `names`x''
		mat rownames V = `names`x''
		
		local i = 0
		forval y = 2/8 {
			di "PREFERRED FIELD: `y'"
			if `x'!=`y' {
				if `y'`x'!=25 & `y'`x'!=26 & `y'`x'!=53 & `y'`x'!=75 & `y'`x'!=51 & `y'`x'!=58 & `y'`x'!=52 {
					local i = `i'+1
					mat b[1,`i']=_b[`y'`x'.over_X_grp]
					mat V[`i',`i']=_se[`y'`x'.over_X_grp]^2
				}
			}	
		}

		qui count `if'  & f2==`x'
		local N = r(N)
		qui sum restance `if' & f2==`x'
		if r(N)>=5 {
			local avg = r(mean)*100
		}

		ereturn post b V, depname(`x') obs(`N')
		times100
		eststo reg`j'
			qui estadd scalar avg=`avg'
	}
	
	local coef "1 "\hspace{10pt}STEM" 2 "\hspace{10pt}Bus. \& Econ." 3 "\hspace{10pt}Law" 4 "\hspace{10pt}Hum., A. and A." 5 "\hspace{10pt}Education" 6 "\hspace{10pt}Welfare" 7 "\hspace{10pt}Medicine" 8 "\hspace{10pt}Social Science"" 
	esttab  reg2 reg3 reg4 reg5 reg6 reg7 reg1 using "$tabs/TableA16.tex", replace ///
		b(1) noomit se order(2 3 4 5 6 8 7) star(* .1 ** .05 *** .01) coef(`coef') ///
		mtitles("B\&E" "Law" "HAA" "Educ." "Welf." "Soc." "STEM") nonum nonote ///
		mgroup("Alternative field", span prefix(\multicolumn{@span}{c}{) suffix(})) ///
		refcat(2 "Preferred field", nolabel) 
		

	* A17
	foreach x in 1 2 3 4 5 6 8 {
		loc ++j
		di "ALTERNATIVE FIELD: `x'"
		qui ivregress 2sls restance (i.opt_X_grp = i.over_X_grp) i.f2##c.dist i.f1##c.over_X_dist i.grp `if'
		if `x'==4 | `x'==1  {
			mat b=J(1,6,.)
			mat V=J(6,6,0)
		}
		if `x'==2 | `x'==3 | `x'==6 | `x'==8 {
			mat b=J(1,5,.)
			mat V=J(5,5,0)
		}
		if `x'==5  {
			mat b=J(1,4,.)
			mat V=J(4,4,0)
		}
		mat colnames b = `names`x''
		mat colnames V = `names`x''
		mat rownames V = `names`x''
		
		local i = 0
		forval y = 2/8 {
			di "PREFERRED FIELD: `y'"
			if `x'!=`y' {
				if `y'`x'!=25 & `y'`x'!=26 & `y'`x'!=53 & `y'`x'!=75 & `y'`x'!=51 & `y'`x'!=58 & `y'`x'!=52 {
					local i = `i'+1
					mat b[1,`i']=_b[`y'`x'.opt_X_grp]
					mat V[`i',`i']=_se[`y'`x'.opt_X_grp]^2
				}
			}	
		}

		qui count `if'  & f2==`x'
		local N = r(N)
		qui sum restance `if' & f2==`x'
		if r(N)>=5 {
			local avg = r(mean)*100
		}

		ereturn post b V, depname(`x') obs(`N')
		times100
		eststo reg`j'
			qui estadd scalar avg=`avg'
	}		

	local coef "1 "\hspace{10pt}STEM" 2 "\hspace{10pt}Bus. \& Econ." 3 "\hspace{10pt}Law" 4 "\hspace{10pt}Hum., A. and A." 5 "\hspace{10pt}Education" 6 "\hspace{10pt}Welfare" 7 "\hspace{10pt}Medicine" 8 "\hspace{10pt}Social Science"" 
	esttab  reg9 reg10 reg11 reg12 reg13 reg14 reg8 using "$tabs/TableA17.tex", replace ///
		b(1) se order(2 3 4 5 6 8 7) star(* .1 ** .05 *** .01) coef(`coef') ///
		mtitles("B\&E" "Law" "HAA" "Educ." "Welf." "Soc." "STEM") nonum nonote ///
		mgroup("Alternative field", span prefix(\multicolumn{@span}{c}{) suffix(})) ///
		refcat(2 "Preferred field", nolabel) 
	
	
********************************************************************************	
	
	* TABLE A18

	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	
	* (1): Gender
	cap drop group
	gen group = sex
	reg restance i.over##i.group##c.dist `if' `opt' 
	test 1.over+1.over#1.group=0
		times100
		eststo reg1
		estadd scalar p = r(p)
		qui count `if' & group==1
		estadd scalar n = r(N) 
	tab kotnavn2 group `if', row
		
	* (2): Father has high education
	cap drop group
	gen group = (audd_niveau_far=="Mellemlange videregående uddannelser / Bachelorer" | audd_niveau_far=="Korte videregående uddannelser" | audd_niveau_far=="Lange videregående uddannelser" | audd_niveau_far=="Ph.d. og forskeruddannelser") if !mi(audd_niveau_far)
	reg restance i.over##i.group##c.dist `if' `opt' 
	test 1.over+1.over#1.group=0
		times100
		eststo reg2
		estadd scalar p = r(p)
		qui count `if' & group==1
		estadd scalar n = r(N) 
	tab kotnavn2 group `if', row	
	
	* (3): Age at application
	cap drop group
	gen group = (alder_yoa<21) 
	reg restance i.over##i.group##c.dist `if' `opt' 
	test 1.over+1.over#1.group=0
		times100
		eststo reg3
		estadd scalar p = r(p)
		qui count `if' & group==1
		estadd scalar n = r(N) 
	tab kotnavn2 group `if', row
	
	* (4): Mathematical track in Upper Secondary School
	cap drop group
	gen group = (audd_0ya=="1145") 
	reg restance i.over##i.group##c.dist `if' `opt' 
	test 1.over+1.over#1.group=0
		times100
		eststo reg4
		estadd scalar p = r(p)
		count `if' & group==1
		estadd scalar n = r(N) 
	tab kotnavn2 group `if', row
	
	* (5): Year of application
	cap drop group
	gen group = (yoa<2000) 
	reg restance i.over##i.group##c.dist `if' `opt' 
	test 1.over+1.over#1.group=0
		times100
		eststo reg5
		estadd scalar p = r(p)
		qui count `if' & group==1
		estadd scalar n = r(N) 
	tab kotnavn2 group `if', row
		
	esttab reg1 reg2 reg3 reg4 reg5 using "$tabs\TableA18.tex", replace ///
		b(2) se nonote keep(1.over 1.over#1.group 1.group) star(* 0.1 ** 0.05 *** 0.01) ///
		coef(1.over "$\mathbbm{1}$(Economics)" 1.group "Group = 1" 1.over#1.group "$\mathbbm{1}$(Economics) $\times$ Group") ///
		mgroup("Men" "Father has" "Age" "Math" "Year", pattern(1 1 1 1 1) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
		mtit(" " "high edu." "18-20" "track" "$<2000$")  ///
		scalar("n \textit{N} in group" "p $p$-val of effect for group") sfmt(0 3)	
	
	
********************************************************************************

* FIGURE A10

	* Panel A
	cap drop restance1_8ya
	egen restance1_8ya = rowmax(rest8_1ya rest8_2ya rest8_3ya rest8_4ya rest8_5ya rest8_6ya rest8_7ya rest8_8ya)
	replace restance1_8ya=0 if mi(restance1_8ya) & inrange(yoa,1995,2006)
	
	cap drop restance9_13ya
	egen restance9_13ya = rowmax(rest8_9ya rest8_10ya rest8_11ya rest8_12ya rest8_13ya)
	replace restance9_13ya=0 if mi(restance9_13ya) & inrange(yoa,1993,2006)
	
	cap drop restance14_18ya
	egen restance14_18ya = rowmax(rest8_14ya rest8_15ya rest8_16ya rest8_17ya rest8_18ya)
	replace restance14_18ya=0 if mi(restance14_18ya) & inrange(yoa,1993,2002)
	
	cap drop restance19_23ya
	egen restance19_23ya = rowmax(rest8_19ya rest8_20ya rest8_21ya rest8_22ya rest8_23ya)
	replace restance19_23ya=0 if mi(restance19_23ya) & inrange(yoa,1993,1997)

	postfile event x b h l g using "$pofi/event", replace
	reg restance1_8ya over dist over_X_dist if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if, robust
	post event (4.5) (_b[over]) (_b[over]+1.65*_se[over]) (_b[over]-1.65*_se[over]) (1)
	reg restance9_13ya over dist over_X_dist if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if, robust
	post event (11) (_b[over]) (_b[over]+1.65*_se[over]) (_b[over]-1.65*_se[over]) (1)
	reg restance14_18ya over dist over_X_dist if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if, robust
	post event (16) (_b[over]) (_b[over]+1.65*_se[over]) (_b[over]-1.65*_se[over]) (1)
	reg restance19_23ya over dist over_X_dist if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if, robust
	post event (21) (_b[over]) (_b[over]+1.65*_se[over]) (_b[over]-1.65*_se[over]) (1)
	
	reg restance1_8ya if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if, robust
	post event (4.5) (_b[_cons]) (.) (.) (2)
	reg restance9_13ya if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if, robust
	post event (11) (_b[_cons]) (.) (.) (2)
	reg restance14_18ya if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if, robust
	post event (16) (_b[_cons]) (.) (.) (2)
	reg restance19_23ya if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if, robust
	post event (21) (_b[_cons]) (.) (.) (2)
	
	postclose event
	
	preserve
		use "$pofi/event", clear
		twoway 	(line b x if g==2, lc(red*0.5) ) ///	
				(scatter b x if g==1, mc(blue*0.5) ) ///
				(rcap h l x if g==1, lc(blue*0.5)), ///
				$layout xsize(20) ysize(20) ///
				xlabel(4.5 "1-8" 11 "9-13" 16 "14-18" 21 "19-23",labsize(small)) ylabel(-.2(0.1)0.1,$lblobs) ///
				xmtick(5(5)20, grid glc(white)) ///
				xtitle("Years since application") ///
				ytitle("Probability of default and delinquency") ///
				legend(ring(0) r(3) pos(7) order(2 3 1) label(1 "Avg. probability") label(2 "Estimated effect") label(3 "90% CI")) ///
				name(fig1, replace)
	restore
	
	* Panel B
	cap postclose event 
	postfile event x b h l g using "$pofi/event", replace
	forval x=1/23 {
		reg in_edu_`x'ya if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if
		post event (`x') (_b[_cons]) (.) (.) (.)
	}
	cap postclose event 
	
	preserve
		use "$pofi/event", clear
		twoway 	(scatter b x, mc(blue*0.5) ), ///
				$layout xsize(20) ysize(20) ///
				xlabel(,$lblobs) ylabel(,$lblobs) ///
				xtitle("Years since application") ///
				ytitle("Share enrolled in education") ///
				legend(off) ///
				name(fig2, replace)
	restore
	
	* Panel C
	local if "f2==2 & (optaget1==1 | optaget2==1) & $if"
	local y "restance19_23ya"
	local xlbl "-10 "-1" -5 "-0.5" 0 "0" 5 "0.5" 10 "1""
	local obs=29
	local dot "msym(O) mc(blue*.5)"

	cap drop bin
	gen 	bin = .
	replace bin = 1  if `if' & inrange(dist10,-20,-19)
	replace bin = 2  if `if' & inrange(dist10,-18,-17)
	replace bin = 3  if `if' & inrange(dist10,-16,-15)
	replace bin = 4  if `if' & inrange(dist10,-14,-13)
	replace bin = 5  if `if' & inrange(dist10,-12,-11)
	replace bin = 6  if `if' & inrange(dist10,-10,-9)
	replace bin = 7  if `if' & inrange(dist10,-8 ,-7)
	replace bin = 8  if `if' & inrange(dist10,-6 ,-5)
	replace bin = 9  if `if' & inrange(dist10,-4 ,-3)
	replace bin = 10 if `if' & inrange(dist10,-2 ,-1)
	replace bin = 11 if `if' & inrange(dist10, 0 ,1)
	replace bin = 12 if `if' & inrange(dist10, 2 ,3)
	replace bin = 13 if `if' & inrange(dist10, 4 ,5)
	replace bin = 14 if `if' & inrange(dist10, 6 ,7)
	replace bin = 15 if `if' & inrange(dist10, 8 ,9)
	replace bin = 16 if `if' & inrange(dist10,10 ,11)
	replace bin = 17 if `if' & inrange(dist10,12 ,13)
	replace bin = 18 if `if' & inrange(dist10,14 ,15)
	replace bin = 19 if `if' & inrange(dist10,16 ,17)
	replace bin = 20 if `if' & inrange(dist10,18 ,19)
	cap drop at_vals
	gen at_vals = _n-21 if _n<42
	lpoly optaget2 dist10 if dist10<0  & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(L1) nogr se(L1se)
	lpoly optaget2 dist10 if dist10>=0 & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(R1) nogr se(R1se)
	lpoly `y'      dist10 if dist10<0  & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(L2) nogr se(L2se)
	lpoly `y'      dist10 if dist10>=0 & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(R2) nogr se(R2se)
	cap postclose fig
	postfile fig x y str5 grph using "$pofi/rd", replace
	levelsof bin, loc(b)
	foreach x in `b' {
		qui count if bin==`x' & !mi(`y')
		if r(N)<=`obs' {
			di "X=`x': FOR FÅ OBSERVATIONER"  
		}
		if r(N)>`obs' {
			sum dist10 if bin==`x', meanonly
			local avg = r(mean)
			qui reg optaget2 if bin==`x' & `if'
			post fig (`avg') (_b[_cons]) ("scat1")
			qui reg `y' if bin==`x' & `if'
			post fig (`avg') (_b[_cons]) ("scat2")
		}
	}
	forval x = -20/20 {
		sum L1 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L1") 
		sum R1 if at_vals==`x', meanonly 
		post fig (`x') (r(mean)) ("R1") 
		sum L2 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L2") 
		sum R2 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("R2") 
		sum L1se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L1se") 
		sum R1se if at_vals==`x', meanonly 
		post fig (`x') (r(mean)) ("R1se") 
		sum L2se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L2se") 
		sum R2se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("R2se") 
	}
	postclose fig
	drop L1* R1* L2* R2* at_vals
		
	preserve
		use "$pofi/rd", clear
		gen y1 = y + 1.65*y[_n+4]
		gen y2 = y - 1.65*y[_n+4]
		twoway 	(rarea 	 y1 y2 x if grph=="L2" & x<=0 & x>=-10, color(gs5%20)) ///
				(rarea 	 y1 y2 x if grph=="R2" & x>=0 & x<=10,  color(gs5%20)) ///
				(scatter y     x if grph=="scat2" & inrange(x,-10,10), `dot')  ///
				(line    y     x if grph=="L2" & x<=0 & x>=-10, lp(solid) lc(gs5)) ///
				(line    y     x if grph=="R2" & x>=0 & x<=10,  lp(solid) lc(gs5)) ///
				,  ///
					xline(0, lp(dash) lc(black)) $layout  xsize(20) ysize(20) ///
					ylabel(`ylbl', $lblobs)  ///
					xlabel(`xlbl', $lblobs) ///
					xtitle("Distance from threshold") ytitle("Share of applicants in default" "19-23 years after application") ///
					legend(off) ///
					name(fig3, replace)
	restore
	
	* Panel D
	cap postclose share
	postfile share x y f using "$pofi/share", replace
	forval x=1993/2006 {
		count if f2==2 & yoa==`x' & $if
		local denom = `r(N)'
		forval y=3/8 {
			qui count if f2==2 & yoa==`x' & $if & f1==`y'
			local share = `r(N)'/`denom'	
			post share (`x') (`share') (`y')
		}
	}
	postclose share

	preserve
		use "$pofi/share", clear
		twoway 	(line y x if f==3, lp(solid)     lc(blue*0.5)) ///
				(line y x if f==4, lp(dash)      lc(orange*0.5)) ///
				(line y x if f==5, lp(dot)       lc(red*0.5)) ///
				(line y x if f==6, lp(longdash)  lc(green*0.5)) ///
				(line y x if f==7, lp(shortdash) lc(purple*0.5)) ///
				(line y x if f==8, lp(dash_dot)  lc(gs5*0.5)), ///
					$layout xsize(20) ysize(20) ///
					xlabel(1993(2)2006,$lblobs) ylabel(0(0.1)0.6,$lblobs) ///
					xtitle("Year of application cohort") ytitle("Share of applicants preferring the field") ///
					legend(ring(0) pos(12) r(2) label(1 "Law") label(2 "Hum., Art & Arch.") label(3 "Education") label(4 "Welfare") label(5 "Medicine") label(6 "Social Science") size(small) symx(7)) name(fig4, replace)
	restore
	
	* Panel E
	tab audd_0ya
	cap drop group*
	local if "if audd_0ya=="1145" | audd_0ya=="1146" | audd_0ya=="1532" | audd_0ya=="1549" | audd_0ya=="4008" | audd_0ya=="5098""
	gen group1 = (audd_0ya=="1145") `if' // Math track
	gen group2 = (audd_0ya=="1146") `if' // Linguistic track
	gen group3 = (audd_0ya=="1532" | audd_0ya=="1549") `if' // HF
	gen group4 = (audd_0ya=="4008" | audd_0ya=="5098") `if' // HHX
	cap postclose event 
	postfile event x b h l g using "$pofi/event", replace
	forval x=1993/2006 {
		forval y=1/4 {
			reg group`y' if yoa==`x' & f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if
		post event (`x') (_b[_cons]) (.) (.) (`y')
		}
	}
	cap postclose event 
	
	preserve
		use "$pofi/event", clear
		twoway 	(line b x if g==1, lp(solid)     lc(blue*0.5)) ///
				(line b x if g==2, lp(dash)      lc(red*0.5)) ///
				(line b x if g==3, lp(dash_dot)  lc(green*0.5)) ///
				(line b x if g==4, lp(longdash)  lc(orange*0.5)), ///
				$layout xsize(20) ysize(20) ///
				xlabel(1995(5)2005,$lblobs) ylabel(,$lblobs) ///
				xtitle("Year of application") ///
				ytitle("Share from Upper Secondary School") ///
				legend(r(4) ring(0) pos(9) label(1 "Mathematical track") label(2 "Linguistic track") label(3 "Higher Preparatory Examination") label(4 "Higher Commercial Examination") size(small) symx(*0.5)) ///
				name(fig5, replace)
	restore
	
	* Panel F
	cap drop group*
	gen group1 = (kotnavn2=="Økonomi") 
	gen group2 = (kotnavn2=="Erhvervsøkonomi-erhvervsret") 
	gen group3 = (kotnavn2=="Almen erhvervsøkonomi(HA)") 
	cap postclose event 
	postfile event x b h l g using "$pofi/event", replace
	forval x=1993/2006 {
		forval y=1/3 {
			reg group`y' if yoa==`x' & f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if
		post event (`x') (_b[_cons]) (.) (.) (`y')
		}
	}
	cap postclose event 
	
	preserve
		use "$pofi/event", clear
		twoway 	(line b x if g==1, lp(solid)     lc(blue*0.5)) ///
				(line b x if g==2, lp(dash)      lc(red*0.5)) ///
				(line b x if g==3, lp(longdash)  lc(green*0.5)), ///
				$layout xsize(20) ysize(20) ///
				xlabel(1995(5)2005,$lblobs) ylabel(0(0.2)0.6,$lblobs) ///
				xtitle("Year of application") ///
				ytitle("Share with the program as alternative") ///
				legend(r(3) ring(0) pos(1) label(1 "Economics") label(2 "Business econ. and law") label(3 "Business econ.") size(small) symx(*0.5)) ///
				name(fig6, replace)
	restore
	

	graph export "$figs\FigureA10A.pdf", replace name(fig1) 
	graph export "$figs\FigureA10B.pdf", replace name(fig2)
	graph export "$figs\FigureA10C.pdf", replace name(fig3)
	graph export "$figs\FigureA10D.pdf", replace name(fig4)
	graph export "$figs\FigureA10E.pdf", replace name(fig5)
	graph export "$figs\FigureA10F.pdf", replace name(fig6)
	graph close
	
********************************************************************************

* TABLE A19

	
	local if "if abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & !mi(f1) & !mi(f2) & completed_fields==1"
	
	qui reg restance        over dist over_X_dist `if' & kot2_field_l2=="Economics" & kot1_field_l2!="Economics", robust
		times100
		eststo reg1
		qui sum restance `if' & kot2_field_l2=="Economics" & kot1_field_l2!="Economics"
		estadd scalar avg=r(mean)*100
	qui reg inc_rank_10ya   over dist over_X_dist `if' & kot2_field_l2=="Economics" & kot1_field_l2!="Economics", robust
		eststo reg2
		qui sum inc_rank_10ya `if' & kot2_field_l2=="Economics" & kot1_field_l2!="Economics"
		estadd scalar avg=r(mean)
	qui reg lico_dum_1      over dist over_X_dist `if' & kot2_field_l2=="Economics" & kot1_field_l2!="Economics", robust
		times100
		eststo reg3
		qui sum lico_dum_1 `if' & kot2_field_l2=="Economics" & kot1_field_l2!="Economics"
		estadd scalar avg=r(mean)*100
	qui reg lico_dum_2      over dist over_X_dist `if' & kot2_field_l2=="Economics" & kot1_field_l2!="Economics", robust
		times100
		eststo reg4
		qui sum lico_dum_2 `if' & kot2_field_l2=="Economics" & kot1_field_l2!="Economics"
		estadd scalar avg=r(mean)*100
	qui reg debt_dum1       over dist over_X_dist `if' & kot2_field_l2=="Economics" & kot1_field_l2!="Economics", robust
		times100
		eststo reg5
		qui sum debt_dum1 `if' & kot2_field_l2=="Economics" & kot1_field_l2!="Economics"
		estadd scalar avg=r(mean)*100
	qui reg stock           over dist over_X_dist `if' & kot2_field_l2=="Economics" & kot1_field_l2!="Economics", robust
		times100
		eststo reg6
		qui sum stock `if' & kot2_field_l2=="Economics" & kot1_field_l2!="Economics"
		estadd scalar avg=r(mean)*100
	esttab reg1 reg2 reg3 reg4 reg5 reg6 using "$tabs\TableA19.tex", replace fragment ///
			b(1) nonote se keep(over) star(* 0.1 ** 0.05 *** 0.01) ///
			coef(over "$\mathbbm{1}$(Economics)") ///
			refcat(over "\multicolumn{7}{l}{\textbf{Panel A}: Economics as alternative}\\%") ///
			mtitle("Default" "Income" "Low deposit" "High rate" "Debt" "Stock") ///
			prehead({\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{6}{c}}\hline\hline\\)
			
	qui reg restance        over dist over_X_dist `if' & kot2_field_l2=="Economics" & f1!=2, robust
		times100
		eststo reg1
		qui sum restance `if' & kot2_field_l2=="Economics" & f1!=2
		estadd scalar avg=r(mean)*100
	qui reg inc_rank_10ya   over dist over_X_dist `if' & kot2_field_l2=="Economics" & f1!=2, robust
		eststo reg2
		qui sum inc_rank_10ya `if' & kot2_field_l2=="Economics" & f1!=2
		estadd scalar avg=r(mean)
	qui reg lico_dum_1      over dist over_X_dist `if' & kot2_field_l2=="Economics" & f1!=2, robust
		times100
		eststo reg3
		qui sum lico_dum_1 `if' & kot2_field_l2=="Economics" & f1!=2
		estadd scalar avg=r(mean)*100
	qui reg lico_dum_2      over dist over_X_dist `if' & kot2_field_l2=="Economics" & f1!=2, robust
		times100
		eststo reg4
		qui sum lico_dum_2 `if' & kot2_field_l2=="Economics" & f1!=2
		estadd scalar avg=r(mean)*100
	qui reg debt_dum1       over dist over_X_dist `if' & kot2_field_l2=="Economics" & f1!=2, robust
		times100
		eststo reg5
		qui sum debt_dum1 `if' & kot2_field_l2=="Economics" & f1!=2
		estadd scalar avg=r(mean)*100
	qui reg stock           over dist over_X_dist `if' & kot2_field_l2=="Economics" & f1!=2, robust
		times100
		eststo reg6
		qui sum stock `if' & kot2_field_l2=="Economics" & f1!=2
		estadd scalar avg=r(mean)*100
	esttab reg1 reg2 reg3 reg4 reg5 reg6 using "$tabs\TableA19.tex", append fragment ///
		b(1) nonote se keep(over) star(* 0.1 ** 0.05 *** 0.01) nomtit nonum ///
		coef(over "$\mathbbm{1}$(Economics)") ///
		refcat(over "\multicolumn{7}{l}{\textbf{Panel B}: Excl. other B\&E programs as preferred}\\%") ///
		postfoot(\hline\hline\end{tabular}}) 
		
	
********************************************************************************
	
* FIGURE 6

	local if "f2==2 & (optaget1==1 | optaget2==1) & $if"
	local y "inc_rank_10ya"
	local xlbl "-15 "-1.5" -10 "-1" -5 "-0.5" 0 "0" 5 "0.5" 10 "1" 15 "1.5""
	local obs=29
	local dot "msym(O) mc(blue*.5)"
	
	cap drop bin
	gen 	bin = .
	replace bin = 1  if `if' & inrange(dist10,-20,-19)
	replace bin = 2  if `if' & inrange(dist10,-18,-17)
	replace bin = 3  if `if' & inrange(dist10,-16,-15)
	replace bin = 4  if `if' & inrange(dist10,-14,-13)
	replace bin = 5  if `if' & inrange(dist10,-12,-11)
	replace bin = 6  if `if' & inrange(dist10,-10,-9)
	replace bin = 7  if `if' & inrange(dist10,-8 ,-7)
	replace bin = 8  if `if' & inrange(dist10,-6 ,-5)
	replace bin = 9  if `if' & inrange(dist10,-4 ,-3)
	replace bin = 10 if `if' & inrange(dist10,-2 ,-1)
	replace bin = 11 if `if' & inrange(dist10, 0 ,1)
	replace bin = 12 if `if' & inrange(dist10, 2 ,3)
	replace bin = 13 if `if' & inrange(dist10, 4 ,5)
	replace bin = 14 if `if' & inrange(dist10, 6 ,7)
	replace bin = 15 if `if' & inrange(dist10, 8 ,9)
	replace bin = 16 if `if' & inrange(dist10,10 ,11)
	replace bin = 17 if `if' & inrange(dist10,12 ,13)
	replace bin = 18 if `if' & inrange(dist10,14 ,15)
	replace bin = 19 if `if' & inrange(dist10,16 ,17)
	replace bin = 20 if `if' & inrange(dist10,18 ,19)
	cap drop at_vals
	gen at_vals = _n-21 if _n<42
	lpoly `y' dist10 if dist10<0  & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(L2) nogr se(L2se)
	lpoly `y' dist10 if dist10>=0 & `if', deg(1) bw(15) kernel(rec) at(at_vals) gen(R2) nogr se(R2se)
	cap postclose fig
	postfile fig x y str5 grph using "$pofi/rd", replace
	levelsof bin, loc(b)
	foreach x in `b' {
		qui count if bin==`x' 
		if r(N)<=`obs' {
			di "X=`x': FOR FÅ OBSERVATIONER"  
		}
		if r(N)>`obs' {
			sum dist10 if bin==`x', meanonly
			local avg = r(mean)
			qui reg optaget2 if bin==`x' & `if'
			post fig (`avg') (_b[_cons]) ("scat1")
			qui reg `y' if bin==`x' & `if'
			post fig (`avg') (_b[_cons]) ("scat2")
		}
	}
	forval x = -15/15 {
		sum L2 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L2") 
		sum R2 if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("R2") 
		sum L2se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("L2se") 
		sum R2se if at_vals==`x', meanonly
		post fig (`x') (r(mean)) ("R2se") 
	}
	postclose fig
	drop L2* R2* at_vals
		
	preserve
		use "$pofi/rd", clear
		gen y1 = y + 1.65*y[_n+2]
		gen y2 = y - 1.65*y[_n+2]
		
		twoway 	(rarea 	 y1 y2 x if grph=="L2" & x<=0, color(gs5%20)) ///
				(rarea 	 y1 y2 x if grph=="R2" & x>=0, color(gs5%20)) ///
				(scatter y     x if grph=="scat2", `dot')  ///
				(line    y     x if grph=="L2" & x<=0, lp(solid) lc(gs5)) ///
				(line    y     x if grph=="R2" & x>=0, lp(solid) lc(gs5)) ///
				,  ///
					xline(0, lp(dash) lc(black)) $layout ///
					xlabel(`xlbl', $lblobs) /// 
					ylabel(50(10)90, $lblobs)  ///
					xtitle("Distance from threshold") ytitle("Income rank 10 years after application") ///
					legend(off) ///
					xsize(20) ysize(11)
	restore

	graph export "$figs\Figure6.pdf",   replace 
	
********************************************************************************	

* TABLE A20

	* Panel A
	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	
	* (1) Income rank
	cap drop temp 
	gen temp 	 = inc_rank_10ya `if'
	replace temp = inc_rank_9ya  `if' & mi(temp)
	replace temp = inc_rank_11ya `if' & mi(temp)
	qui reg temp over dist over_X_dist `if' `opt'
		eststo reg1
		estadd scalar avg=_b[_cons]
		estadd scalar bw=1.5
	* (2) Income rank, Law preferred
	qui reg temp over dist over_X_dist `if' & f1==3 `opt'
		eststo reg2
		estadd scalar avg=_b[_cons]
		estadd scalar bw=1.5
	* (3) Income rank, Social Science preferred
	qui reg temp over dist over_X_dist `if' & f1==8 `opt'
		eststo reg3
		estadd scalar avg=_b[_cons]
		estadd scalar bw=1.5
	* (4) Top 10%
	cap drop temp 
	gen 	temp = (inrange(inc_rank_10ya,91,100)) `if'
	replace temp = (inrange(inc_rank_9ya,91,100))  `if' & mi(temp) 
	replace temp = (inrange(inc_rank_11ya,91,100)) `if' & mi(temp)
	qui reg temp over dist over_X_dist `if' `opt'
		times100
		eststo reg4
		estadd scalar avg=_b[_cons]
		estadd scalar bw=1.5
	* (5) Bottom 50%
	cap drop temp 
	gen 	temp = (inrange(inc_rank_10ya,1,50)) `if'
	replace temp = (inrange(inc_rank_9ya,1,50))  `if' & mi(temp) 
	replace temp = (inrange(inc_rank_11ya,1,50)) `if' & mi(temp)
	qui reg temp over dist over_X_dist `if' `opt'
		times100
		eststo reg5
		estadd scalar avg=_b[_cons]
		estadd scalar bw=1.5
	* (6) 
	qui reg persindk_1_9ya over dist over_X_dist `if' `opt'
		eststo reg6
		estadd scalar avg=_b[_cons]
		estadd scalar bw=1.5
	* (7)
	qui reg persindk_9_11ya over dist over_X_dist `if' `opt'
		eststo reg7
		estadd scalar avg=_b[_cons]
		estadd scalar bw=1.5
	* (8)
	cap drop temp
	gen temp = persindk_sd_8_12ya
	qui sum temp, d
	replace temp = r(p99) if temp>r(p99) & !mi(temp)
	replace temp = r(p1) if temp<r(p1)  & !mi(temp)
	qui reg temp over dist over_X_dist `if' `opt'
		eststo reg8
		estadd scalar avg=_b[_cons]
		estadd scalar bw=1.5
	* (9)
	qui reg erhvervsindk_1_9ya over dist over_X_dist `if' `opt'
		eststo reg9
		estadd scalar avg=_b[_cons]
		estadd scalar bw=1.5
	* (10)
	qui reg erhvervsindk_9_11ya over dist over_X_dist `if' `opt'
		eststo reg10
		estadd scalar avg=_b[_cons]
		estadd scalar bw=1.5
	* (11)
	qui reg household_inc_9_11ya over dist over_X_dist `if' `opt'
		eststo reg11
		estadd scalar avg=_b[_cons]
		estadd scalar bw=1.5
	
	esttab reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 reg9 reg10 reg11 using "$tabs/TableA20.tex", replace fragment ///
	b(1) se nonum star(* 0.1 ** 0.05 *** 0.01) nonote keep(over) ///
	scalar("avg Baseline")  /// 
	mgroup("Income rank" "Total income (1000)" "Labor inc. (1000)" "HH inc.", pattern(1 0 0 0 0 1 0 0 1 0 1) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) ///
	coef(over "$\mathbbm{1}$(Economics)") refcat(over "\multicolumn{12}{l}{\textbf{Panel A: 1.5 bandwidth}}\\%", label(" "))	///
	mlabel("All" "Law" "Soc. Sci." "Top 10\%" "Bottom 50\%" " " " " "Std. Dev." " " " " "(1000)") ///
	prehead({\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{11}{c}}\hline\hline) ///
	posthead(& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) & (10) & (11) \\)
	
	* Panel B
	local if "if f2==2 & (dist10==-1 | dist10==1) & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	
	* (1) Income rank
	cap drop temp 
	gen temp 	 = inc_rank_10ya `if'
	replace temp = inc_rank_9ya  `if' & mi(temp)
	replace temp = inc_rank_11ya `if' & mi(temp)
	qui reg temp over `if' `opt'
		eststo reg1
		estadd scalar avg=_b[_cons]
	* (2) Income rank, Law preferred
	qui reg temp over `if' & f1==3 `opt'
		eststo reg2
		estadd scalar avg=_b[_cons]
	* (3) Income rank, Social Science preferred
	qui reg temp over `if' & f1==8 `opt'
		eststo reg3
		estadd scalar avg=_b[_cons]
	* (4) Top 10%
	cap drop temp 
	gen 	temp = (inrange(inc_rank_10ya,91,100)) `if'
	replace temp = (inrange(inc_rank_9ya,91,100))  `if' & mi(temp) 
	replace temp = (inrange(inc_rank_11ya,91,100)) `if' & mi(temp)
	qui reg temp over `if' `opt'
		times100
		eststo reg4
		estadd scalar avg=_b[_cons]
	* (5) Bottom 50%
	cap drop temp 
	gen 	temp = (inrange(inc_rank_10ya,1,50)) `if'
	replace temp = (inrange(inc_rank_9ya,1,50))  `if' & mi(temp) 
	replace temp = (inrange(inc_rank_11ya,1,50)) `if' & mi(temp)
	qui reg temp over `if' `opt'
		times100
		eststo reg5
		estadd scalar avg=_b[_cons]
	* (6) 
	qui reg persindk_1_9ya over `if' `opt'
		eststo reg6
		estadd scalar avg=_b[_cons]
	* (7)
	qui reg persindk_9_11ya over `if' `opt'
		eststo reg7
		estadd scalar avg=_b[_cons]
	* (8)
	cap drop temp
	gen temp = persindk_sd_8_12ya
	qui sum temp, d
	replace temp = r(p99) if temp>r(p99) & !mi(temp)
	replace temp = r(p1) if temp<r(p1)  & !mi(temp)
	qui reg temp over `if' `opt'
		eststo reg8
		estadd scalar avg=_b[_cons]
	* (9)
	qui reg erhvervsindk_1_9ya over `if' `opt'
		eststo reg9
		estadd scalar avg=_b[_cons]
	* (10)
	qui reg erhvervsindk_9_11ya over `if' `opt'
		eststo reg10
		estadd scalar avg=_b[_cons]
	* (11)
	qui reg household_inc_9_11ya over `if' `opt'
		eststo reg11
		estadd scalar avg=_b[_cons]
	
	esttab reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 reg9 reg10 reg11 using "$tabs/TableA20.tex", fragment append ///
	b(1) se nonum star(* 0.1 ** 0.05 *** 0.01) nonote keep(over) nomtit ///
	scalar("avg Baseline")  ///
	coef(over "$\mathbbm{1}$(Economics)") refcat(over "\multicolumn{12}{l}{\textbf{Panel B: Local randomization}}\\%", label(" "))	
	
	* Panel C
	local if "if f2==2 & dist10!=0 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", "
	cap drop temp_x
	gen temp_x = -dist10
	
	* (1) Income rank
	cap drop temp 
	gen temp 	 = inc_rank_10ya `if'
	replace temp = inc_rank_9ya  `if' & mi(temp)
	replace temp = inc_rank_11ya `if' & mi(temp)
	rdrobust temp temp_x `if' `opt'
		eststo reg1
		qui estadd scalar avg = e(tau_cl_l)
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
		estadd local unit "Rank" 
		estadd local period "10" 
	* (2) Income rank, Law preferred
	qui rdrobust temp temp_x `if' & f1==3 `opt'
		eststo reg2
		qui estadd scalar avg = e(tau_cl_l)
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
		estadd local unit "Rank" 
		estadd local period "10" 
	* (3) Income rank, Social Science preferred
	qui rdrobust temp temp_x `if' & f1==8 `opt'
		eststo reg3
		qui estadd scalar avg = e(tau_cl_l)
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
		estadd local unit "Rank" 
		estadd local period "10" 
	* (4) Top 10%
	cap drop temp 
	gen 	temp = (inrange(inc_rank_10ya,91,100)) `if'
	replace temp = (inrange(inc_rank_9ya,91,100))  `if' & mi(temp) 
	replace temp = (inrange(inc_rank_11ya,91,100)) `if' & mi(temp)
	qui rdrobust temp temp_x `if' `opt'
		times100
		eststo reg4
		qui estadd scalar avg = e(tau_cl_l)*100
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
		estadd local unit "\%-point" 
		estadd local period "10" 
	* (5) Bottom 50%
	cap drop temp 
	gen 	temp = (inrange(inc_rank_10ya,1,50)) `if'
	replace temp = (inrange(inc_rank_9ya,1,50))  `if' & mi(temp) 
	replace temp = (inrange(inc_rank_11ya,1,50)) `if' & mi(temp)
	qui rdrobust temp temp_x `if' `opt'
		times100
		eststo reg5
		qui estadd scalar avg = e(tau_cl_l)*100
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
		estadd local unit "\%-point" 
		estadd local period "10" 
	* (6) 
	qui rdrobust persindk_1_9ya temp_x `if' `opt'
		eststo reg6
		qui estadd scalar avg = e(tau_cl_l)
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
		estadd local unit "DKK" 
		estadd local period "1-9" 
	* (7)
	qui rdrobust persindk_9_11ya temp_x `if' `opt'
		eststo reg7
		qui estadd scalar avg = e(tau_cl_l)
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
		estadd local unit "DKK" 
		estadd local period "9-11" 
	* (8)
	cap drop temp
	gen temp = persindk_sd_8_12ya
	qui sum temp, d
	replace temp = r(p99) if temp>r(p99) & !mi(temp)
	replace temp = r(p1) if temp<r(p1)  & !mi(temp)
	sum temp,d
	qui rdrobust temp temp_x `if' `opt'
		eststo reg8
		qui estadd scalar avg = e(tau_cl_l)
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
		estadd local unit "DKK" 
		estadd local period "8-12" 
	* (9)
	qui rdrobust erhvervsindk_1_9ya temp_x `if' `opt'
		eststo reg9
		qui estadd scalar avg = e(tau_cl_l)
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
		estadd local unit "DKK" 
		estadd local period "1-9" 
	* (10)
	qui rdrobust erhvervsindk_9_11ya temp_x `if' `opt'
		eststo reg10
		qui estadd scalar avg = e(tau_cl_l)
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
		estadd local unit "DKK" 
		estadd local period "9-11" 
	* (11)
	qui rdrobust household_inc_9_11ya temp_x `if' `opt'
		eststo reg11
		qui estadd scalar avg = e(tau_cl_l)
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
		estadd local unit "DKK" 
		estadd local period "9-11" 
	
	esttab reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 reg9 reg10 reg11 using "$tabs/TableA20.tex", fragment append ///
	b(1) se nonum star(* 0.1 ** 0.05 *** 0.01) nonote keep(RD_Estimate) nomtit ///
	scalar("avg Baseline" "bw Bandwidth" "unit \hline Unit" "period Years after application")  ///
	coef(RD_Estimate "$\mathbbm{1}$(Economics)") refcat(RD_Estimate "\multicolumn{12}{l}{\textbf{Panel C: MSE Optimal Bandwidth}}\\%", label(" "))	///
	postfoot(\hline\hline\end{tabular}})
			
			
********************************************************************************

* FIGURE A11

	* Panel A
	cap postclose inc 
	postfile inc x p10 p25 p50 p75 p90 i using "$pofi/inc", replace
	forval x=1993/2006 {
		_dstpctile_fast persindk_10ya if yoa==`x' & f2==2 & $if, p(10 25 50 75 90)
		post inc (`x'-0.15) (r(r1)) (r(r2)) (r(r3)) (r(r4)) (r(r5)) (1)
		_dstpctile_fast erhvervsindk_10ya if yoa==`x' & f2==2 & $if, p(10 25 50 75 90)
		post inc (`x'+0.15) (r(r1)) (r(r2)) (r(r3)) (r(r4)) (r(r5)) (2)
	}
	cap postclose inc 

	preserve
		use "$pofi/inc", clear
		twoway 	(rcap    p10 p90 x if i==1, lc(blue*0.5)) ///
				(rbar    p25 p75 x if i==1, col(blue*0.5) barw(0.25)) ///
				(scatter p50     x if i==1, mc(blue*0.75) ms(o)) ///
				(rcap    p10 p90 x if i==2, lc(red*0.5)) ///
				(rbar    p25 p75 x if i==2, col(red*0.5) barw(0.25)) ///
				(scatter p50     x if i==2, mc(red*0.75) ms(o)), ///
					$layout xsize(20) ysize(20) ///
					xlabel(1993(2)2006,$lblobs) ylabel(0(200)900,$lblobs) ///
					xtitle("Year of application cohort") ytitle("Income 10 years after application" "(1000 DKK in 2015 prices)") ///
					legend(order(3 2 1 6 5 4) ring(0) pos(12) r(2) label(3 "Total inc. P50") label(2 "P25/P75") label(1 "P10/P90") label(6 "Labor inc. P50") label(5 "P25/P75") label(4 "P10/P90") symx(10) size(small)) ///
					name(fig1, replace)
	restore

	* Panel B
	frame change first_time_applicants
	
	sum inc_rank_10ya if !mi(audd_n)
	sum inc_rank_10ya if audd_n==2
	
	
	cap postclose barplot
	postfile barplot c b h l using "$pofi\barplot", replace
	forval x=1/8 {
		reg inc_rank_10ya if audd_n==`x'
		post barplot (`x') (_b[_cons]) (_b[_cons]+1.96*_se[_cons]) (_b[_cons]-1.96*_se[_cons])
	}
	postclose barplot

	preserve
		use "$pofi\barplot", clear
		replace c = -8 if c == 4
		replace c = -7 if c == 6
		replace c = -6 if c == 8
		replace c = -5 if c == 5
		replace c = -4 if c == 2
		replace c = -3 if c == 1
		replace c = -2 if c == 3
		replace c = -1 if c == 7
		
		twoway 	(bar b c if c<0, barw(.5) col(blue*.5)), ///
				$layout xsize(20) ysize(20) legend(off) ///
				xlabel(-8 "Hum., Art & Arch." -4 "Bus. and Econ." -7 "Welfare" -6 "Social Science" -3 "STEM" -5 "Education" -2 "Law" -1 "Medicine" , angle(45) $lblobs) ///
				ylabel(40(10)80, $lblobs) ///
				ytitle("Income rank 10 years after application") xtitle("") ///
				name(fig2, replace)
	restore
	
	* Panel C
	sum inc_rank_10ya if audd_field2_10ya=="Economics" 
	sum inc_rank_10ya if audd_field2_10ya=="Law" 
	sum inc_rank_10ya if audd_field2_10ya=="Political sciences and civics" 
	
	local opts "percent start(0) width(5) fi(50) col(%50)"
	histogram inc_rank_10ya if audd_field2_10ya=="Economics" , ///
		$layout xsize(20) ysize(20) `opts' bc(blue*0.5)  ///
		xlabel(,$lblobs) ylabel(,$lblobs) ///
		xline(68, lc(red*0.5) lw(0.5)) xline(73, lc(blue*0.5) lw(0.5)) xline(75, lc(green*0.5) lw(0.5)) ///
		addplot( ///
			histogram inc_rank_10ya if audd_field2_10ya=="Law",                           `opts' bc(green*0.5)  || ///
			histogram inc_rank_10ya if audd_field2_10ya=="Political sciences and civics", `opts' bc(red*0.5)  ///
		) ///
		legend(ring(0) pos(11) label(1 "Economics") label(2 "Law") label(3 "Political Science")) ///
		xtitle("Income rank within cohort 10 years after application") ///
		name(fig3, replace)
				
		graph export "$figs\FigureA11A.pdf", replace name(fig1) 
		graph export "$figs\FigureA11B.pdf", replace name(fig2)
		graph export "$figs\FigureA11C.pdf", replace name(fig3) 
		graph close

		
********************************************************************************

* FIGURE A12: Income

frame change binding_sample2
	
	local if "if abs(dist10)<=15 & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	local outcome "inc_rank_10ya"
	local ci=1.65

	cap postclose coef_plot
	postfile coef_plot x b h l using "$pofi/coef_plot", replace
	
		cap drop temp1 
		cap drop temp2
		gen temp1 =  1-over
		gen temp2 = over

		reg `outcome' i.temp2##c.dist `if' & f2==1
			post coef_plot (1) (_b[1.temp2]) (_b[1.temp2]+`ci'*_se[1.temp2]) (_b[1.temp2]-`ci'*_se[1.temp2])
			
		reg `outcome' i.temp1##c.dist `if' & f1==7 
			post coef_plot (3) (_b[1.temp1]) (_b[1.temp1]+`ci'*_se[1.temp1]) (_b[1.temp1]-`ci'*_se[1.temp1])

		reg `outcome' i.temp2##c.dist `if' & f2==2 
			post coef_plot (5) (_b[1.temp2]) (_b[1.temp2]+`ci'*_se[1.temp2]) (_b[1.temp2]-`ci'*_se[1.temp2])
		reg `outcome' i.temp1##c.dist `if' & f1==2 
			post coef_plot (6) (_b[1.temp1]) (_b[1.temp1]+`ci'*_se[1.temp1]) (_b[1.temp1]-`ci'*_se[1.temp1])
			
		reg `outcome' i.temp2##c.dist `if' & f2==3 
			post coef_plot (8) (_b[1.temp2]) (_b[1.temp2]+`ci'*_se[1.temp2]) (_b[1.temp2]-`ci'*_se[1.temp2])
		reg `outcome' i.temp1##c.dist `if' & f1==3 
			post coef_plot (9) (_b[1.temp1]) (_b[1.temp1]+`ci'*_se[1.temp1]) (_b[1.temp1]-`ci'*_se[1.temp1])
		
		reg `outcome' i.temp2##c.dist `if' & f2==4 
			post coef_plot (11) (_b[1.temp2]) (_b[1.temp2]+`ci'*_se[1.temp2]) (_b[1.temp2]-`ci'*_se[1.temp2])
		reg `outcome' i.temp1##c.dist `if' & f1==4 
			post coef_plot (12) (_b[1.temp1]) (_b[1.temp1]+`ci'*_se[1.temp1]) (_b[1.temp1]-`ci'*_se[1.temp1])
		
		reg `outcome' i.temp2##c.dist `if' & f2==5 
			post coef_plot (14) (_b[1.temp2]) (_b[1.temp2]+`ci'*_se[1.temp2]) (_b[1.temp2]-`ci'*_se[1.temp2])
		reg `outcome' i.temp1##c.dist `if' & f1==5 
			post coef_plot (15) (_b[1.temp1]) (_b[1.temp1]+`ci'*_se[1.temp1]) (_b[1.temp1]-`ci'*_se[1.temp1])
		
		reg `outcome' i.temp2##c.dist `if' & f2==6 
			post coef_plot (17) (_b[1.temp2]) (_b[1.temp2]+`ci'*_se[1.temp2]) (_b[1.temp2]-`ci'*_se[1.temp2])
		reg `outcome' i.temp1##c.dist `if' & f1==6 
			post coef_plot (18) (_b[1.temp1]) (_b[1.temp1]+`ci'*_se[1.temp1]) (_b[1.temp1]-`ci'*_se[1.temp1])
		
		reg `outcome' i.temp2##c.dist `if' & f2==8 
			post coef_plot (20) (_b[1.temp2]) (_b[1.temp2]+`ci'*_se[1.temp2]) (_b[1.temp2]-`ci'*_se[1.temp2])
		reg `outcome' i.temp1##c.dist `if' & f1==8 
			post coef_plot (21) (_b[1.temp1]) (_b[1.temp1]+`ci'*_se[1.temp1]) (_b[1.temp1]-`ci'*_se[1.temp1])
	postclose  coef_plot
		
	preserve
		use "$pofi/coef_plot", clear
		twoway 	(bar b x if x==1 | x==5 | x==8 | x==11 | x==14 | x==17 | x==20, horizontal col(blue*.5) )  /// Alternative
				(bar b x if x==3 | x==6 | x==9 | x==12 | x==15 | x==18 | x==21, horizontal col(red*.5) )   /// Preferred
				(rcap h l x, horizontal lc(gs5)), ///
				$layout xsize(20) ysize(20) ///
				legend(order(1 2 3) r(1)  pos(6) label(1 "Alternative") label(2 "Preferred") label(3 "90% CI") size(vsmall) symx(*.4)) ///
				ylabel(1 "STEM" 5.5 "Bus. & Econ." 8.5 "Law" 11.5 "H, A & A" 14.5 "Education" 17.5 "Welfare" 3 "Medicine" 20.5 "Soc. Sci.", angle(360) labsize(small)) ///
				xlabel(, labsize(small)) ///
				ytitle("") xtitle("Effect on income rank of admission")
	restore 
	
	graph export  "$figs\FigureA12.pdf", replace
	graph close		

********************************************************************************

* TABLE 3

	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"

	cap drop stock*
	gen stock = (kursakt_10ya+oblakt_2015p_10ya>1500)  if !mi(kursakt_10ya)
	replace stock = 0 if mi(stock)

	qui reg lico_dum_1 over dist over_X_dist `if' `opt'
		times100
		eststo reg1
		qui estadd scalar avg=_b[_cons]
	qui reg lico_dum_2 over dist over_X_dist `if' `opt'
		times100
		eststo reg2
		qui estadd scalar avg=_b[_cons]
	qui reg debt_dum1 over dist over_X_dist `if' `opt'
		times100
		eststo reg3
		qui estadd scalar avg=_b[_cons]
	qui reg debt_dum2 over dist over_X_dist `if' `opt'
		times100
		eststo reg4
		qui estadd scalar avg=_b[_cons]
	qui reg stock over dist over_X_dist `if' `opt'
		times100
		eststo reg5
		qui estadd scalar avg=_b[_cons]
	
	esttab reg1 reg2 reg3 reg4 reg5 using "$tabs\Table3.tex", replace ///
		b(1) se star(* 0.1 ** 0.05 *** 0.01) keep(over) nonum nonote ///
		coef(over "$\mathbbm{1}$(Economics)") ///
		mgroup("Low bank" "High interest" "Has bank" "High debt-to-inc."   "Owns stocks", pattern(1 1 1 1 1) span) ///
		mtitle("deposit (=1)"  "rate (=1)" "debt (=1)" "ratio (=1)" "and bonds (=1)") ///
		scalar("avg Baseline") 	
	
********************************************************************************
	
* TABLE A18	

	* Panel A
	local if "if f2==2 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if & (dist10==1 | dist10==-1)"
	local opt ", robust"
	qui reg lico_dum_1 over `if' `opt'
		times100
		eststo reg1
		qui estadd scalar avg=_b[_cons]
	qui reg lico_dum_2 over `if' `opt'
		times100
		eststo reg2
		qui estadd scalar avg=_b[_cons]
	qui reg debt_dum1 over `if' `opt'
		times100
		eststo reg3
		qui estadd scalar avg=_b[_cons]
	qui reg debt_dum2 over `if' `opt'
		times100
		eststo reg4
		qui estadd scalar avg=_b[_cons]
	qui reg stock over  `if' `opt'
		times100
		eststo reg5
		qui estadd scalar avg=_b[_cons]
		
	esttab reg1 reg2 reg3 reg4 reg5 using "$tabs\TableA21.tex", replace fragment ///
		b(1) se star(* 0.1 ** 0.05 *** 0.01) keep(over) nonum nonote ///
		coef(over "$\mathbbm{1}$(Economics)") ///
		mgroup("Low bank" "High interest" "Has bank" "High debt-to-inc."   "Owns stocks", pattern(1 1 1 1 1) span) ///
		mtitle("deposit (=1)"  "rate (=1)" "debt (=1)" "ratio (=1)" "and bonds (=1)") ///
		scalar("avg Baseline") refcat(over "\multicolumn{6}{l}{\textbf{Panel A: Local randomization}} \\ %", label(" ")) ///
		prehead({\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{5}{c}}\hline\hline)
	
	* Panel B
	local if "if f2==2 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if & dist10!=0"
	local opt ","
	cap drop temp
	gen temp = -dist10
	rdrobust lico_dum_1 temp `if' `opt'
		times100
		eststo reg1
		qui estadd scalar avg = e(tau_cl_l)*100
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
	rdrobust lico_dum_2 temp `if' `opt'
		times100
		eststo reg2
		qui estadd scalar avg = e(tau_cl_l)*100
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
	qui rdrobust debt_dum1 temp `if' `opt'
		times100
		eststo reg3
		qui estadd scalar avg = e(tau_cl_l)*100
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
	qui rdrobust debt_dum2 temp `if' `opt'
		times100
		eststo reg4
		qui estadd scalar avg = e(tau_cl_l)*100
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
	qui rdrobust stock temp `if' `opt'
		times100
		eststo reg5
		qui estadd scalar avg = e(tau_cl_l)*100
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
		
	esttab reg1 reg2 reg3 reg4 reg5 using "$tabs\TableA21.tex", fragment append ///
		b(1) se star(* 0.1 ** 0.05 *** 0.01)  nonum nonote nomtit ///
		coef(RD_Estimate "$\mathbbm{1}$(Economics)") ///
		scalar("avg Baseline" "bw Bandwidth") refcat(RD_Estimate "\multicolumn{6}{l}{\textbf{Panel B: MSE Optimal Bandwidth}} \\ %", label(" ")) ///
		postfoot(\hline\hline\end{tabular}})	
	
********************************************************************************

* TABLE A22 AND A23

	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	*(1) 
	cap drop temp
	qui gen temp = (oblgaeld_2015p_10ya>0) if !mi(oblgaeld_2015p_10ya) & !mi(persindk_10ya)
	qui replace temp = 0 if mi(temp)	
	qui reg temp over dist over_X_dist `if' `opt'
		times100
		eststo reg1, title("Mortgage debt (=1)")	
		sum temp  `if', meanonly
		estadd scalar avg=_b[_cons]
	* (2)
	cap drop temp 
	gen temp = ejendom_10ya
	replace temp=0 if mi(temp)
	qui reg temp over dist over_X_dist `if' `opt'
		times100
		eststo reg2, title("Real estate (=1)")
		sum temp `if', meanonly
		estadd scalar avg=_b[_cons]
	* (3)
	cap drop temp 
	qui gen temp = blb_rest8_2016
	qui sum temp, d
	qui replace temp = r(p99) if !mi(temp) & temp>r(p99)
	qui replace temp=0 if mi(temp)
	qui replace temp=temp/1000
	qui reg temp over dist over_X_dist `if' `opt'
		eststo reg3, title("Amount in default (2016)")	
		sum temp  `if', meanonly
		estadd scalar avg=_b[_cons]
	* (4)
	cap drop temp 
	qui gen temp = rest_total
	qui sum temp, d
	qui replace temp = r(p99) if !mi(temp) & temp>r(p99)
	qui reg temp over dist over_X_dist `if' `opt'
		eststo reg4, title("Number of defaults")
		sum temp  `if', meanonly
		estadd scalar avg=_b[_cons]
	* (5)
	cap drop temp 
	qui gen temp = max_rente_10ya
	qui sum temp, d
	qui replace temp = r(p99) if !mi(temp) & temp>r(p99)
	qui replace temp = r(p1)  if !mi(temp) & temp<r(p1)
	qui reg temp over dist over_X_dist `if' `opt'
		times100
		eststo reg5, title("Marginal interest rate")
		sum temp  `if', meanonly
		estadd scalar avg=_b[_cons]
	* (6)
	cap drop temp 
	qui gen temp = bankgaeld_10ya
	replace temp=0 if mi(temp)
	qui sum temp, d
	qui replace temp = r(p99) if !mi(temp) & temp>r(p99)
	qui reg temp over dist over_X_dist `if' `opt'
		eststo reg6, title("Bank debt")
		sum temp  `if', meanonly
		estadd scalar avg=_b[_cons]
	* (7)
	cap drop temp 
	qui gen temp = bankgaeld_10ya/persindk_10ya
	replace temp=0 if mi(temp)
	qui sum temp, d
	qui replace temp = r(p99) if !mi(temp) & temp>r(p99)
	reg temp over dist over_X_dist `if' `opt'
		times100
		eststo reg7, title("Debt-to-inc. ratio")
		sum temp  `if', meanonly
		estadd scalar avg=_b[_cons]
		
	* (8)
	cap drop temp 
	qui gen temp = (gaeld_til_off_2015p_2016>0) if !mi(gaeld_til_off_2015p_2016)
	replace temp=0 if mi(temp)
	qui reg temp over dist over_X_dist `if' `opt'
		times100
		eststo reg8, title("Public debt 2016 (=1)")
		sum temp  `if', meanonly
		estadd scalar avg=_b[_cons]
	* (9)
	cap drop temp 
	qui gen temp = kursakt_10ya+oblakt_2015p_10ya // Note kursakt_10ya is also in 2015 prices
	replace temp=0 if mi(temp)
	qui sum temp, d
	qui replace temp = r(p99) if !mi(temp) & temp>r(p99)
	qui replace temp=temp/1000
	reg temp over dist over_X_dist `if' `opt'
		eststo reg9, title("Stocks")
		sum temp  `if', meanonly
		estadd scalar avg=_b[_cons]
	* (10)
	cap drop temp 
	qui gen temp = la_10ya
	replace temp=0 if mi(temp)
	qui sum temp, d
	qui replace temp = r(p99) if !mi(temp) & temp>r(p99)
	qui reg temp over dist over_X_dist `if' `opt'
		eststo reg10, title("Liquid assets")
		sum temp  `if', meanonly
		estadd scalar avg=_b[_cons]
	* (11)
	cap drop temp 
	qui gen temp = bankakt_10ya
	replace temp=0 if mi(temp)
	qui sum temp, d
	qui replace temp = r(p99) if !mi(temp) & temp>r(p99)
	qui reg temp over dist over_X_dist `if' `opt'
		eststo reg11, title("Bank deposits")
		sum temp  `if', meanonly
		estadd scalar avg=_b[_cons]
	
********************************************************************************

	* (12)
	cap drop temp 
	qui gen temp = pensionsdepot_2016
	replace temp = 0 if mi(pensionsdepot_2016)
	qui sum temp, d
	qui replace temp = r(p99) if !mi(temp) & temp>r(p99)
	qui replace temp = r(p1)  if !mi(temp) & temp<r(p1)
	qui reg temp over dist over_X_dist `if' `opt'
		eststo reg12, title("Pension")
		sum temp  `if', meanonly
		estadd scalar avg=_b[_cons]
		
	* (13)
	cap drop temp
	gen temp = arbpens_10ya 
	replace temp = 0 if mi(temp) & !mi(arbpens_bidrag_10ya)
	qui reg temp over dist over_X_dist `if'  `opt'
		times100
		eststo reg13, title("Pension")
		sum temp `if', meanonly
		estadd scalar avg=_b[_cons]
		
	* (14)
	cap drop temp 
	qui gen temp = arbpens_bidrag_10ya/1000 
	sum temp, d
	qui replace temp = r(p99) if !mi(temp) & temp>r(p99)
	qui replace temp = r(p1)  if !mi(temp) & temp<r(p1)
	qui reg temp over dist over_X_dist `if' `opt'
		eststo reg14, title("Pension")
		sum temp  `if', meanonly
		estadd scalar avg=_b[_cons]
		
	* (15)
	cap drop temp
	gen temp = pens_10ya 
	replace temp = 0 if mi(temp) & !mi(pens_bidrag_10ya)
	qui reg temp over dist over_X_dist `if'  `opt'
		times100
		eststo reg15, title("Pension")
		sum temp `if', meanonly
		estadd scalar avg=_b[_cons]
		
	* (16)
	cap drop temp 
	qui gen temp = pens_bidrag_10ya/1000 
	sum temp, d
	qui replace temp = r(p99) if !mi(temp) & temp>r(p99)
	qui replace temp = r(p1)  if !mi(temp) & temp<r(p1)
	qui reg temp over dist over_X_dist `if' `opt'
		eststo reg16, title("Pension")
		sum temp  `if', meanonly
		estadd scalar avg=_b[_cons]
	
	esttab reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 reg9 reg10 reg11 using "$tabs\TableA22.tex", replace ///
			b(1) nonote se keep(over) star(* 0.1 ** 0.05 *** 0.01) ///
			mtit("\%" "\%" "1000 DKK" " " "\%" "1000 DKK" "\%" "\%" "1000 DKK" "1000 DKK" "1000 DKK") ///
			mgroup("Mortgage debt (=1)" "Real estate (=1)" "Amount in default" "Number of defaults" "Marginal interest rate" "Bank debt" "Debt-to-inc. ratio" "Public debt (=1)" "Stocks and bonds" "Liquid assets" "Bank deposits" , patter(1 1 1 1 1 1 1 1 1 1 1) span prefix(\multicolumn{@span}{c}{) suffix(})) ///
			scalar("avg Baseline") ///
			coef(over "$\mathbbm{1}$(Economics)")
			
	esttab reg12 reg13 reg14 reg15 reg16 using "$tabs\TableA23.tex", replace ///
			b(1) nonote se keep(over) star(* 0.1 ** 0.05 *** 0.01) ///
			mtit("1000 DKK" "(=1)" "1000 DKK" "(=1)" "1000 DKK") ///
			mgroup("Pension savings 2016" "Employer contributions" "Private contributions", patter(1 1 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) end(\cmidrule(lr){3-4}\cmidrule(lr){5-6})) ///
			scalar("avg Baseline") ///
			coef(over "$\mathbbm{1}$(Economics)")
				
				
********************************************************************************

* TABLE 4

	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	local i=0
	foreach y in unemp_dum1 unemp_dum2 unemp_dum3 self_empl_10ya priv_sec bra_finans_10ya {	
		local i = `i'+1
		cap drop temp 
		gen temp = `y'
		replace temp=0 if mi(temp)
		qui reg temp over dist over_X_dist `if' `opt'
			times100
			eststo reg`i'
			qui estadd scalar avg=_b[_cons]	
			if `i'==3 {
				estadd local per "1-9"
			}
			if `i'!=3 {
				estadd local per "10"
			}
	}
		
	esttab reg1 reg2 reg3 reg4 reg5 reg6 using "$tabs\Table4.tex", replace ///
		b(1) se star(* 0.1 ** 0.05 *** 0.01) keep(over) nonote ///
		coef(over "$\mathbbm{1}$(Economics)") ///
		mgroup("Unemployment (=1)" "Self-" "Private" "Finance \&", pattern(1 0 0 1 1 1) span prefix(\multicolumn{@span}{c}{) suffix(})  end(\cline{2-4})) ///
		mtitle("$>$ 3 months" "November" " $>$ 6 months" "employed (=1)" "sector (=1)" "insurance (=1)") ///
		scalar("avg Baseline" "per Period") 

		
********************************************************************************
		
* TABLE A24

	* Panel A
	local if "if f2==2 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if & (dist10==1 | dist10==-1)"
	local opt ", robust"
	local i=0
	foreach y in unemp_dum1 unemp_dum2 unemp_dum3 self_empl_10ya priv_sec bra_finans_10ya {	
		local i = `i'+1
		cap drop temp 
		gen temp = `y'
		replace temp=0 if mi(temp)
		qui reg temp over `if' `opt'
			times100
			eststo reg`i'
			qui estadd scalar avg=_b[_cons]	
	}
		
	esttab reg1 reg2 reg3 reg4 reg5 reg6 using "$tabs\TableA24.tex", replace fragment ///
		b(1) se star(* 0.1 ** 0.05 *** 0.01) keep(over) nonum nonote ///
		coef(over "$\mathbbm{1}$(Economics)") ///
		mgroup("Unemployment (=1)" "Self-" "Private" "Finance \&", pattern(1 0 0 1 1 1) span prefix(\multicolumn{@span}{c}{) suffix(})  end(\cline{2-4})) ///
		mtitle("$>$ 3 months" "November" " $>$ 6 months" "employed (=1)" "sector (=1)" "insurance (=1)") ///
		scalar("avg Baseline") refcat(over "\textbf{Panel A: Local randomization}", label(" ")) ///
		prehead({\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{6}{c}}\hline\hline)
		
	* Panel B
	local if "if f2==2 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if & dist10!=0"
	local opt ","
	cap drop temp_x
	gen temp_x = -dist10
	local i=0
	foreach y in unemp_dum1 unemp_dum2 unemp_dum3 self_empl_10ya priv_sec bra_finans_10ya {	
		local i = `i'+1
		cap drop temp_y
		gen temp_y = `y'
		replace temp_y=0 if mi(temp_y)
		qui rdrobust temp_y temp_x `if' `opt'
			times100
			eststo reg`i'
			qui estadd scalar avg = e(tau_cl_l)*100
			qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
			qui estadd scalar bw = floor(e(h_l))/10
			if `i'==3 {
				estadd local per "1-9"
			}
			if `i'!=3 {
				estadd local per "10"
			}
	}
		
	esttab reg1 reg2 reg3 reg4 reg5 reg6 using "$tabs\TableA24.tex", fragment append ///
		b(1) se star(* 0.1 ** 0.05 *** 0.01) nonum nonote nomtit ///
		coef(RD_Estimate "$\mathbbm{1}$(Economics") ///
		scalar("avg Baseline" "bw Bandwidth" "per \hline Period") refcat(RD_Estimate "\textbf{Panel B: MSE Optimal Bandwidth}", label(" ")) ///
		postfoot(\hline\hline\end{tabular}})

	
********************************************************************************

* TABLE A25

	local if "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"

	qui reg study_emp_high over dist over_X_dist `if' `opt'
		times100
		eststo reg1
		estadd scalar avg=_b[_cons]

	qui reg study_emp_finance over dist over_X_dist `if' `opt'
		times100
		eststo reg2
		estadd scalar avg=_b[_cons]

	qui reg study_emp_edu over dist over_X_dist `if' `opt'
		times100
		eststo reg3
		estadd scalar avg=_b[_cons]

	qui reg study_emp_rd over dist over_X_dist `if' `opt'
		times100
		eststo reg4
		estadd scalar avg=_b[_cons]

	qui reg au over dist over_X_dist `if' `opt'
		times100
		eststo reg5
		estadd scalar avg=_b[_cons]

	qui reg ku over dist over_X_dist `if' `opt'
		times100
		eststo reg6
		estadd scalar avg=_b[_cons]
		
	qui reg bss over dist over_X_dist `if' `opt'
		times100
		eststo reg7
		estadd scalar avg=_b[_cons]
		
	qui reg cbs over dist over_X_dist `if' `opt'
		times100
		eststo reg8
		estadd scalar avg=_b[_cons]

	esttab reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 using "$tabs/TableA25.tex", replace fragment  ///
			b(1) se nonote keep(over) nonum ///
			scalar("avg Baseline") star(* 0.1 ** 0.05 *** 0.01)  ///
			mgroup("Student job" "Institution", pattern(1 0 0 0 1 0 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) end(\cmidrule(lr){2-5}\cmidrule(lr){6-9}))  ///
			coef(over "$\mathbbm{1}$(Economics)") ///
			refcat(over "\textbf{Panel A: Broad field}", nolabel) ///
			mtit("High-skill" "Finance" "Education" "R\&D" "AU" "KU" "BSS" "CBS") ///
			prehead(\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{8}{c}}\hline\hline) 

	local if "if kot2_field_l2=="Economics" & f1!=2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"

	qui reg study_emp_high over dist over_X_dist `if' `opt'
		times100
		eststo reg1
		estadd scalar avg=_b[_cons]

	qui reg study_emp_finance over dist over_X_dist `if' `opt'
		times100
		eststo reg2
		estadd scalar avg=_b[_cons]

	qui reg study_emp_edu over dist over_X_dist `if' `opt'
		times100
		eststo reg3
		estadd scalar avg=_b[_cons]

	qui reg study_emp_rd over dist over_X_dist `if' `opt'
		times100
		eststo reg4
		estadd scalar avg=_b[_cons]

	qui reg au over dist over_X_dist `if' `opt'
		times100
		eststo reg5
		estadd scalar avg=_b[_cons]

	qui reg ku over dist over_X_dist `if' `opt'
		times100
		eststo reg6
		estadd scalar avg=_b[_cons]

	qui reg bss over dist over_X_dist `if' `opt'
		times100
		eststo reg7
		estadd scalar avg=_b[_cons]
		
	qui reg cbs over dist over_X_dist `if' `opt'
		times100
		eststo reg8
		estadd scalar avg=_b[_cons]

	esttab reg1 reg2 reg3 reg4 reg5 reg6 reg7 reg8 using "$tabs/TableA25.tex", append fragment ///
			b(1) se nonote keep(over) nonum nomtit ///
			scalar("avg Baseline") star(* 0.1 ** 0.05 *** 0.01)  ///
			coef(over "$\mathbbm{1}$(Economics)") ///
			refcat(over "\textbf{Panel B: Detailed field}", nolabel) ///
			postfoot(\hline\hline\end{tabular})

			
********************************************************************************

* TABLE A26

	cap drop math_peers
	gen 	math_peers = kot1_mat1 if optaget1==1
	replace math_peers = kot2_mat1 if optaget2==1

	cap drop match1
	gen match1=(f1==audd_n_efalle_10ya) 
	cap drop match2
	gen match2=(f2==audd_n_efalle_10ya) 
	cap drop match3
	gen match3=(audd_n_efalle_10ya==2)  
		
	cap drop tmp
	gen tmp = -(over-1)
	
	* Panel A
	local if1 "if         abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local if2 "if f2==2 & abs(dist10)<=15 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	
	qui reg match1 i.tmp##c.dist `if1' `opt' 
		times100
		eststo reg1
		estadd scalar avg=_b[_cons]
	qui reg match2 over dist over_X_dist `if1' `opt' 
		times100
		eststo reg2
		estadd scalar avg=_b[_cons]
	qui reg match3 over dist over_X_dist `if2' `opt'
		times100
		eststo reg3
		estadd scalar avg=_b[_cons]
	qui reg math_peers over dist over_X_dist `if2' `opt'
		times100
		eststo reg4
		estadd scalar avg=_b[_cons]
		
	esttab reg1 reg2 reg3 reg4 using "$tabs\TableA26.tex", replace fragment ///
		b(1) nonote se keep(over) star(* 0.1 ** 0.05 *** 0.01) ///
		coef(over "$\mathbbm{1}$(Admission)") ///
		mtit("Preferred" "Alternative" "Bus. \& Econ." " ") ///
		mgroup("Partner's completed field of study" "Math. peers", pattern(1 0 0 1) span prefix(\multicolumn{@span}{c}{) suffix(}) end(\cmidrule(lr){2-4})) ///
		scalar("avg Baseline")	rename(1.tmp over) refcat(over "\multicolumn{5}{l}{\textbf{Panel A: 1.5 bandwidth}}\\%",nolabel) ///
		prehead({\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{4}{c}}\hline\hline)
		
	* Panel B
	local if1 "if         (dist10==1 | dist10==-1) & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local if2 "if f2==2 & (dist10==1 | dist10==-1) & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt ", robust"
	
	qui reg match1 tmp `if1' `opt' 
		times100
		eststo reg1
		estadd scalar avg=_b[_cons]
	qui reg match2 over dist over_X_dist `if1' `opt' 
		times100
		eststo reg2
		estadd scalar avg=_b[_cons]
	qui reg match3 over dist over_X_dist `if2' `opt'
		times100
		eststo reg3
		estadd scalar avg=_b[_cons]
	qui reg math_peers over dist over_X_dist `if2' `opt'
		times100
		eststo reg4
		estadd scalar avg=_b[_cons]
		
	esttab reg1 reg2 reg3 reg4 using "$tabs\TableA26.tex", append fragment ///
		b(1) nonote se keep(over) star(* 0.1 ** 0.05 *** 0.01) ///
		coef(over "$\mathbbm{1}$(Admission)") ///
		nomtit nonum ///
		scalar("avg Baseline")	rename(tmp over) refcat(over "\multicolumn{5}{l}{\textbf{Panel B: Local randomization}}\\%",nolabel)
		
	* Panel C
	local if1 "if         dist10!=0 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local if2 "if f2==2 & dist10!=0 & !mi(restance) & !mi(sex) & !mi(yoa) & !mi(aldersgrp) & !mi(inc_dum) & (optaget1==1 | optaget2==1) & $if"
	local opt " "
	
	cap drop tmp 
	gen tmp = -dist10
	
	qui rdrobust match1 dist10 `if1' `opt' 
		times100
		eststo reg1
		qui estadd scalar avg = e(tau_cl_l)*100
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
	qui rdrobust match2 tmp `if1' `opt' 
		times100
		eststo reg2
		qui estadd scalar avg = e(tau_cl_l)*100
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
	qui rdrobust match3 tmp `if2' `opt'
		times100
		eststo reg3
		qui estadd scalar avg = e(tau_cl_l)*100
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
	qui rdrobust math_peers tmp `if2' `opt'
		times100
		eststo reg4
		qui estadd scalar avg = e(tau_cl_l)*100
		qui estadd scalar N = e(N_h_l)+e(N_h_r), replace
		qui estadd scalar bw = floor(e(h_l))/10
		
	esttab reg1 reg2 reg3 reg4 using "$tabs\TableA26.tex", append fragment ///
		b(1) nonote nonum se keep(RD_Estimate) star(* 0.1 ** 0.05 *** 0.01) ///
		coef(RD_Estimate "$\mathbbm{1}$(Admission)") ///
		nomtit ///
		scalar("avg Baseline" "bw Bandwidth") refcat(RD_Estimate "\multicolumn{5}{l}{\textbf{Panel C: MSE Optimal bandwidth}}\\%",nolabel) ///
		postfoot(\hline\hline\end{tabular}})
	
********************************************************************************